Page 1 of 1
Anyway of doing an and statement in PREG
Posted: Wed Sep 09, 2009 10:33 am
by ctrLogicDotNet
I'm a bit new to regular expressions and am using preg_match from PREG in PHP...
Here's what I'm trying to do:
I want the first character to be: _ (underscore)
I don't want the following character to either be:
I or D if followed by _ (underscore) or
F (does'nt matter what character is following the F)
Anyway of doing this in one regular expression? I suppose so, and that I just can't see it or am still not used enough to PREG syntax...
I tried something like
the: , (coma) would be an and statement that would be useful if it exists
Thanks to anyone helping me on this one
Re: Anyway of doing an and statement in PREG
Posted: Wed Sep 09, 2009 1:09 pm
by John Cartwright
Re: Anyway of doing an and statement in PREG
Posted: Wed Sep 09, 2009 3:13 pm
by ctrLogicDotNet
Thanks
I suppose that I'll find the answer to what I'm looking for in your suggested link...
I'll have a look at it tonight and post back if it resolved my issue...
Re: Anyway of doing an and statement in PREG
Posted: Wed Sep 09, 2009 8:54 pm
by ridgerunner
This one should do the trick...
Code: Select all
if (preg_match('/
^_ # I want the first character to be: _ (underscore)
(?! # I dont want the following character to either be:
[ID]_ | # I or D if followed by _ (underscore) or
F # F (doesnt matter what character is following the F)
).*$ # match rest of line
/x',
$text)) {
# Successful match
} else {
# Match attempt failed
}
And regarding the AND question... Yes you can most certainly apply AND logic. The way that lookahead works is that it is an
assertion at a position in the subject string - it does not move the regex engine forward one character. It simply checks to see if the following regex matches (positive lookahead) or doesn't match (negative lookahead) at this position. Since each assertion does not move the regex engine position, you can have multiple assertions all in a row - all using AND logic. Here is an example which illustrates this principle by matching a line containing one word AND this word begins with a vowel AND does not begin with "Ant" AND also does not begin with "Emu"...
Code: Select all
if (preg_match('/
^ # at the position at the beginning of string,
(?= \w++ ) # make sure that it begins with a word
(?= [AEIOU] ) # AND this word must start with a vowel
(?! Ant ) # AND it does NOT start with "Ant"
(?! Emu ) # AND it also does NOT start with "Emu"
\w++ # all assertions succeeded. match the word
$ # match end of string
/x',
$text)) {
# Successful match
} else {
# Match attempt failed
}
Note that each of these lookahead assertions can be as simple or complex as needed. Each one is tested in turn and all of them must be true before the regex engine moves ahead. With this in mind here is an alternate solution to your specific question...
Code: Select all
if (preg_match('/
^_ # the first character is _
(?![ID]_) # which is not followed by I or D if followed by _
(?!F) # AND is also not followed by F
.*$ # if assertions true, match rest of line
/x',
$text)) {
# Successful match
} else {
# Match attempt failed
}
Regards from Salt Lake City
Re: Anyway of doing an and statement in PREG
Posted: Mon Sep 14, 2009 1:54 pm
by prometheuzz
ridgerunner, it isn't mentioned as much as it should: great contributions man! Good example code, even better explanations! Keep it up!
Cheers,
Bart.