Take anything, but absolutely not one phrase

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

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Take anything, but absolutely not one phrase

Post by Todd_Z »

in a preg_match_all I have a (.*?), I want it to take all characters, however, I want it to absolutely stop no matter what at "Employer" is there something like [^"Employer"]*?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to Regex.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm thinking about it, but I haven't found a good way yet. My reference book is at work....

However can you explain a little more about the problem. It seems like you could do something with strpos to check for "Employer" first.

Or if you know that Employer will always be present and you want to grab everything before it, just do "\(.*)?Employer(.*)?\" to match everything before and everything after, excluding the "Employer" text...

Do either of these ideas fit your needs?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You could mean this is more than one way. Do you mean you want EVERYTING up UNTIL the word "employer" or do you mean you want EVERYTING EXCEPT the word exployer. Two different regex (one's a replace and one's a match).

Code: Select all

//Up util
preg_match('/^(.*?)(?:Employer)?.*$/is', $string, $matches);
$extract = $matches[1];

//Everything except... bah, I'm not even gonna write it... str_replace() would even do it!
;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

what is the colon doing there d11?

Code: Select all

(?:Employer)
that colon just before Employer...what does that do?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

the ?: makes the () not return a value.
Post Reply