Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I disregard spaces while doing matching in Perl ? I tried the "x" option, but it doesnt work :
 $output_line = /$words_to_match/ix ;
Example:
$words_to_match = "Hello World";
Then I want it to match all of the following:
$output_line = " Hello World"; $output_line = "Hello World "; $output_line = " Hello World";
Any ideas that may help ? Thanks!

Replies are listed 'Best First'.
Re: disregard spaces while matching ?
by hv (Prior) on Aug 04, 2004 at 23:22 UTC

    The //x option is designed to make the actual regexp easier to read, so literal spaces are ignored; in this case, though, you're not using literal spaces but instead interpolating a variable as the pattern; moreover you don't want the spaces ignored but rather mapped to an arbitrary sequence of white space.

    You can't do that without providing the pattern to do it. It isn't entirely clear from your description what that pattern should be, but one possibility would be:

    $words_to_match =~ s/^\s*|\s+|\s*$/\\s*/g; $output_line =~ /$words_to_match/i; ...

    Note that this replaces the beginning, the end, and any sequence of whitespace within the variable with 'match zero or more whitespace characters', so it would also match "helloworld"; if you want interior breaks to require at least one space in a matching string, you need to handle the endpoints separately from the interior breaks:

    $words_to_match =~ s/^\s*|\s*$/\\s*/g; $words_to_match =~ s/\s+/\\s+/g; $output_line =~ /$words_to_match/i; ...

    Hugo

Re: disregard spaces while matching ?
by Ovid (Cardinal) on Aug 04, 2004 at 22:42 UTC

    The exact approach will depend upon the problem you;re trying to solve.

    my $match = qr/Hello\s*World/; while (<DATA>) { print if /$match/; } __DATA__ Hello World Hello World Hello World .

    Optionally, you could copy the string to match and remove the spaces prior to the match, but whether or not such a destructive behavior is desireable again depends upon your needs.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: disregard spaces while matching ?
by graff (Chancellor) on Aug 04, 2004 at 22:40 UTC
    My first instinct would be to make the regex like this:
    $words_to_match = qr/\s*Hello\s+World\s*/; # use \s* or \s+ as needed @trials = ("Hello World", " Hello World ", "Hello,World"); for ( @trials ) { $result = ( /$words_to_match/ ) ? "matches" : "does not match"; print "$_ $result\n"; }
Re: disregard spaces while matching ?
by murugu (Curate) on Aug 05, 2004 at 03:55 UTC

    The 'x' modifier is used for writing regular legibly by including spaces inside the regular expression, but it ignore ordinary white spaces.

    u try this,

    $words_to_match = 'Hello\s+World'; $output_line = /$words_to_match/ix ;
Re: disregard spaces while matching ?
by gorillaman (Acolyte) on Aug 05, 2004 at 09:08 UTC
    This too will work, just a minor variation of the above.
    if( $test_string =~ /hello(\s+)world/ig ) { print "FOUND IT!"; } else { print "DIDNT FIND IT!"; }

    The "i" will ignore case, and the "g" will allow it to match more than once, or globally.

      No need to surround the \s+ with parenthesis. The only reason to use parenthesis is if you want to "capture" that part of the match. Using your example:
      if( $test_string =~ /hello(\s+)world/ig ) { $spaces_between = $1; ### $1 comes from matching the ### expression in parenthesis $ct_spaces_between = length $spaces_between; print "FOUND IT! The number of spaces between 'hello' and 'world +' was $ct_spaces_between\n"; }
      You should be able to use just /hello\s+world/ as your regular expression.

      HTH.