Ignoring Regular Expression Spaces (perl)

I use Perl regular expressions. How would I go to ignore spaces and still run a test to see if the string matches. For instance.

$var = "         hello     ";     #I want var to igonore whitespace and still match
if($var =~ m/hello/)
{



} 
+3
source share
4 answers

what you have there should correspond only to a fine. the regex will match any template greeting event, so as long as it sees hello somewhere in $ var it will match

On the other hand, if you want to strictly abide by what you ignore, you must bind your string from start to finish

if($var =~ m/^\s*hello\s*$/) {
}

and if you have a few words in your template

if($var =~ m/^\s*hello\s+world\s*$/) {
}

\ s * 0 , \s + 1 . ^ , $ .

+8

, Perl , . , , , . , / ..

\b. /\ bbook\b/matches

"book. "
"book "
"-book"

"booking"
"ebook"
+1

/^\shello\s $/

0

, , if.

s/[\h\v]+/ /g;
0

Source: https://habr.com/ru/post/1739730/


All Articles