Forum Moderators: coopster

Message Too Old, No Replies

Regex question

preg_match

         

Code Sentinel

6:29 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



I finally got a fairly good grasp on regex but some of the syntax is still somewhat confusing.

One I would like clarification on is the "(?!"

I used it as follows: /^(?!car¦truck)/i

is this right or should it be /^(?!car¦!truck)/i

My gut tells me the former is correct because I can't find any mention of '!' anywhere in the PCRE PHP pages except after '(?' but when I read the pattern I find I can read it as such.. which is corrrect?

-------------------------
/^(?!car¦truck)/i
-------------------------
start of line does not match car or truck

or

start of line does not match car or matches truck

ergophobe

9:54 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(?!car)

would be a zero-width negative lookahead for "car" (so a match would occur if the next three characters were something other than "car" and it would not advance the cursor in event of a match).

Without testing, I believe that

(?!(car¦truck))

should give you your "line does not start with car or truck" match.

Code Sentinel

11:24 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



your pattern makes a lot of sense.. one of those "why didn't I think of that" as soon as I saw it.

although I have tried both ways and so far they do the exact same thing, that is they do what I expect it to, although my data I'm looping through so far has only had either of the two pattern matches.

I need to wrap my head around the "lookarounds" and zerowidth stuff more thoroughly.

I like your code better though.. it makes sense so I'll just use that :P

Thanks