Student86
Student86

Reputation: 1

Regexp to match everything before certain characters in php

I need the regex expression to match everything from beginning till I encounter one of these characters ; or [ or { or ( In case these characters are not there then the whole word needs to be matched..

I am using preg_match("/(.*?)(?=(;|\[|\(|{|))/", $word , $val);

The first group should be there as I need the matched value in $val. This is php. Can somebody please help me with this?

Upvotes: 0

Views: 690

Answers (2)

Anomie
Anomie

Reputation: 94834

preg_match("/^([^[{(;]*)/", $word , $val);

Upvotes: 2

Mario
Mario

Reputation: 36537

"/(.*?)[;\\[\\{\\($]/" should do the trick and be a lot easier. Note that you'll have to escape characters that have special meanings in regular expressions. $ matches the end of the line/string (depending on parameters).

Edit: Think Anomie's solution might even be a tid bit faster due to only matching one sub expression.

Upvotes: 0

Related Questions