开发者

Python regex to PHP?

I'm trying to convert a Python script into PHP.

The following 2 regular expressions work in Python:

'/\*\*(开发者_StackOverflow社区[\w\n\(\)\[\]\.\*\'\"\-#|,@{}_<>=:/ ]+?)\*/'
'(?:\* ([\w\d\(\),\.\'\"\-\:#|/ ]+)|(?<= @)(\w+)(?: (.+))?)'

...however, if I try to run them in PHP I get:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ']'

How come?


The reason is that PHP expects delimiters around its regexes, so it treats the first and second slash as delimiters and tries to parse what follows as modifiers.

Surround your regex with new delimiters and try again (I also removed some unnecessary backslashes):

'%/\*\*([\w\n()\[\].*\'"#|,@{}_<>=:/ -]+?)\*/%'
'%(?:\* ([\w\d(),.\'"\:#|/ -]+)|(?<= @)(\w+)(?: (.+))?)%'

Hint: Use RegexBuddy to do these things. It will take a regex written in language A and convert it to language B for you.


PCRE (including preg_match_all) requires a pattern boundary. You need to wrap the entire pattern in /, @, #, %, or many other possible options. I suggest % as it doesn't look like you are using it in either pattern, that is:

%(?:\* ([\w\d\(\),\.\'\"\-\:#|/ ]+)|(?<= @)(\w+)(?: (.+))?)%

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜