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