Page 2 of 2
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 3:59 pm
by prometheuzz
Leandro-AL wrote:For example i want this text to be matched:
"the quick
brown fox jumped
over the lazy
dog"
And anything that is not made up of only lowercase letters (for simplicity) not to be matched. When i use this pattern:
...
Then don't use the multi-line option. Try this instead:
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 4:20 pm
by Leandro-AL
Ok that worked thanks a million!
However what i'm not getting is why do all the tutorials say that \m is for multiline and from
this site for example it says:
If you have a string consisting of multiple lines, like first line\nsecond line (where \n indicates a line break), it is often desirable to work with lines, rather than the entire string. Therefore, all the regex engines discussed in this tutorial have the option to expand the meaning of both anchors. ^ can then match at the start of the string (before the f in the above string), as well as after each line break (between \n and s). Likewise, $ will still match at the end of the string (after the last e), and also before every line break (between e and \n).
So this is sort of confusing. i thought that by placing \m in the end the ^ and $ will check what's in between them for each line... or?
Cause according to my thought the first line would match for "first line" and the second would skip "\n" and go to "second line"
Re: Pattern checks only last line of multiline block :(
Posted: Mon Sep 29, 2008 1:19 am
by prometheuzz
Leandro-AL wrote:Ok that worked thanks a million!
You're welcome.
Leandro-AL wrote:So this is sort of confusing. i thought that by placing \m in the end the ^ and $ will check what's in between them for each line... or?
Cause according to my thought the first line would match for "first line" and the second would skip "\n" and go to "second line"
No, by doing
/^[a-z]+$/m the preg_match(...) function just looks if there's a match in the input string. And it finds "second line". However, if you want to match the entire string, not just separate lines. So you have no need to use the multi-line option: you want ^ to be the start of the string (NOT the start of each line) and you want $ to be the end of the string (NOT the end of each line), hence the final regex:
/^[a-z\s]+$/ where
\s will match the spaces and new line characters (and tabs).
Does that make sense? If not, don't hesitate to ask for clarification!
HTH
Re: Pattern checks only last line of multiline block :(
Posted: Mon Sep 29, 2008 6:04 am
by Leandro-AL
So with \m it looks for at least one match in the entire string and if it's found returns true?
Thanks for your help.
Re: Pattern checks only last line of multiline block :(
Posted: Mon Sep 29, 2008 7:04 am
by prometheuzz
Leandro-AL wrote:So with \m it looks for at least one match in the entire string and if it's found returns true?
Thanks for your help.
Not quite. preg_match(...) will look for any match in the target string. It doesn''t matches the entire string so when you use the multi line option, it will look for any line in your string that matches the pattern between ^ and $ and without the multi line option, your entire string will be matched (if ^ and $ are used).
Re: Pattern checks only last line of multiline block :(
Posted: Mon Sep 29, 2008 7:16 am
by prometheuzz
One more example.
Take the string
'aa\n12\nbb', with multi line, the text is interpreted like this:
(I added the beginning of line (^) and end of line ($) for clarity)
So,
'/^\d+$/m' will match
'^12$'.
Now, without the multi line option, the text will be interpreted like this:
and it is clear that the regex
'/^\d+$/' will not match it.
Re: Pattern checks only last line of multiline block :(
Posted: Wed Oct 01, 2008 6:19 am
by Leandro-AL
I think i've got it. Thank you
Re: Pattern checks only last line of multiline block :(
Posted: Wed Oct 01, 2008 6:24 am
by prometheuzz
Leandro-AL wrote:I think i've got it. Thank you
Good to hear it. You're welcome.