Re: the only difference is the 'x' after '/g'



[top posting corrected - please fix your quoting style]
Tom Arnall wrote:
On Thursday 30 March 2006 05:15 am, Hans Meier (John Doe) wrote:
tom arnall am Donnerstag, 30. März 2006 12.36:
the following code:

my (@main);
$_="
From a
From: b
From: c
From: d
";
@main = /From [^\n]*?\n.*?(From: .*?\n).*?/gx;
print "@main";
print "------------------------------\n";
@main = /From [^\n]*?\n.*?(From: .*?\n).*?/g;
print "@main";

produces:

From: b
From: d
------------------------------
From: b

the only difference between the two regex lines is the 'x' after '/g' in
the first of the two regex lines.

There's an additional difference on the semantic level: spaces in the 1st
regex are irrelevant. Look at the first regex space: in the 2nd regex, it
matches (only) in "From a", but not in "From: c", whereas the 1st regex
matches "From a" *and* "From: c".

/From[^\n]*?\n.*?(From: .*?\n).*?/gx;
and
/From[^\n]*?\n.*?(From: .*?\n).*?/g;
produce both the same output.

i need the blank in 'From [' etc in order to distinguish it from the strings
with 'From:' one solution to the problem, btw, is to use '\s' instead of a
literal blank. does this behavior rank as a bug in perl?

You're not making any sense. You added the /x modifier. The ONLY
effect this has is to make the pattern match ignore white spaces. If
you don't want the pattern match to ignore white spaces, why did you
add the /x modifier? This is not a bug - this is the EXACT effect this
modifier is supposed to have!

You need to read the documentation, rather than just typing random
characters into your code and then complaining when they have the exact
effect they're supposed to have.

If for some reason you want to use the /x modifier, but still want to
recognize a particular whitespace character, prepend that whitespace
with a backslash.

Paul Lalli

.



Relevant Pages

  • Re: search and replace
    ... The /o modifier tells the internal regex compiler that, after this regex has been compiled 'o'nce, it is never to be compiled again. ... The /s modifier often carries the mnemonic "so you can match your string as though it were a single line" and that all of the sudden makes people think all sorts of crazy things. ...
    (perl.beginners)
  • Re: Regular Expresssion - Matching over multiple lines
    ... No modifier to a regex changes the greediness of the quantifiers ... three regexes have the /gc modifier, when the first one fails, it'll try ... CPAN ID: PINYAN [Need a programmer? ...
    (perl.beginners)
  • Fwd: regex matching conditionally
    ... Subject: regex matching conditionally ... The /o modifier only applies if you have variables in the regular expression. ... optimizes and only compiles static regex once? ... will give rise to dom! ...
    (perl.beginners)
  • Re: Nested {}
    ... The /x modifier is so that I can have extra whitespace, ... contents as PART of the regex to match. ... CPAN ID: PINYAN [Need a programmer? ...
    (perl.beginners)