Page 1 of 2
Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 8:50 am
by Leandro-AL
Hello! I'm trying to use this pattern to filter form input (the message body) that will be emailed to me from a "contact us" form.
The pattern is:
Code: Select all
$clean['match'] = '/^[A-Za-z0-9\/\'\- .,!?]+$/m';
But it seems to check only the last line. When i use it in a single line mode (i use the same pattern for the subject - without the m) it works fine.
What am i doing wrong?
Thanks!
Edit: for some reason the pattern is not appearing well in the code... Here it is in plain text:
$clean['match'] = '
/^[A-Za-z0-9\/\'\- .,!?]+$/m';
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 10:03 am
by prometheuzz
When the 'm' (multi-line option) is used, every line in your text matches ^.*$, so the ^ does not only match the start of the string and $ does not match only the end of the string.
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 11:35 am
by Leandro-AL
Hmm... so what can i do? Any suggestions? Maybe remove ^ $ ?
I want to create a pattern that will match letters, numbers, punctuation and some more characters (including apostrophe) through a block of code as can be an email body.
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 12:40 pm
by Leandro-AL
Well removing ^ and $ messed things up even further. For some reason the g modifier doesn't work, i get an "uknown modifier" error.
I am checking my patterns at this site
http://gskinner.com/RegExr/ and with gm it works but the g won't work in my site...
There's obviously something small that i'm missing here, it can't be that difficult to accomplish this. Plz help!

Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:22 pm
by VladSun
The dot matches a single character, without caring what that character is. The only exception are newline characters.
So by default, the dot is short for the negated character class [^\n] (UNIX regex flavors) or [^\r\n] (Windows regex flavors).
That's why
or
will match the first line of a multi line text.
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:48 pm
by Leandro-AL
Ok guys sorry im sort of a newbie here and i don't totally understand... so how would i write a pattern that checks multiple lines if they contain, say, only lowercase letters [a-z].
Cause you keep bringing up the dot although i dont have it in my pattern so this is confusing me a bit...
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:51 pm
by VladSun
If you mean "the last line should contain only lowercase letters" then you are almost there

Combine your regexp with mine
Otherwise :
http://www.google.com/search?ie=UTF-8&o ... p+tutorial

Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:52 pm
by Leandro-AL
/^[A-Za-z0-9\/\'\- .,!?].+$/m
?
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:55 pm
by VladSun
Sorry, bu you really need to know at least the basics of regexps ...
Read some tutorials, please.
I answered your first question, but it seems that you'll need a lot more of regexps ... So...
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:57 pm
by Leandro-AL
If you mean "the last line should contain only lowercase letters" then you are almost there
No, i mean i want to make sure that the message from the first letter to the last and from the first line to the last is only made of letters (for simplicity).
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 2:59 pm
by Leandro-AL
Actually i have read a lot of tutorials and i've got this book that has a whole chapter on it too. I have managed everything else except this!
If you could be nice enough to write me the pattern code?

Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 3:03 pm
by VladSun
The regexp you want is so elementary that I simply can't believe you've read these tutorials...
Don't take me wrong, I rarely refuse to help people in this forum, but I think that it is more important to one to learn something, instead of copy-paste solutions ...
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 3:07 pm
by Leandro-AL
And i already said that i've got some knowledge on regex (more or less as much as i need) and that i'm obviously missing something small here and that i've already tried searching about it.
Sometimes it's better to give people a 10 letter piece of code than referring them to a "regexp tutorial" google search link.
Thanks anyway.
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 3:24 pm
by prometheuzz
Leandro-AL wrote:Actually i have read a lot of tutorials and i've got this book that has a whole chapter on it too. I have managed everything else except this!

...
I don't know exactly what the problem is. Could you post the some examples that need to be positively matched and some example text that should not?
Re: Pattern checks only last line of multiline block :(
Posted: Sun Sep 28, 2008 3:36 pm
by Leandro-AL
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:
only the last line is checked, so as long as the last line contains only lowercase letters, the whole text flags for valid, which shouldn't happen. This thing here shouldn't flag valid but it does:
"@#wfsdfsdfqsdfWF
<some nasty javascript here>
34r3#RE
dog"