Pattern checks only last line of multiline block :(

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Pattern checks only last line of multiline block :(

Post 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';
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pattern checks only last line of multiline block :(

Post 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.
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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.
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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! :(
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Pattern checks only last line of multiline block :(

Post by VladSun »

Code: Select all

/.+$/
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

Code: Select all

/^.+$/m
or

Code: Select all

/^.*$/m
will match the first line of a multi line text.
There are 10 types of people in this world, those who understand binary and those who don't
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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...
Last edited by Leandro-AL on Sun Sep 28, 2008 2:51 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Pattern checks only last line of multiline block :(

Post 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
;)
Last edited by VladSun on Sun Sep 28, 2008 2:52 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post by Leandro-AL »

/^[A-Za-z0-9\/\'\- .,!?].+$/m


?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Pattern checks only last line of multiline block :(

Post 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...
There are 10 types of people in this world, those who understand binary and those who don't
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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).
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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? :P
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Pattern checks only last line of multiline block :(

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Pattern checks only last line of multiline block :(

Post 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?
Leandro-AL
Forum Newbie
Posts: 23
Joined: Thu Sep 04, 2008 10:05 am
Location: Albania

Re: Pattern checks only last line of multiline block :(

Post 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:

Code: Select all

/^[a-z]+$/m
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"
Post Reply