开发者

Regexp to match everything before certain characters in php

开发者 https://www.devze.com 2023-02-17 14:21 出处:网络
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 nee

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?


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


"/(.*?)[;\\[\\{\\($]/" 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消