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+)(?: (.+))?)%
精彩评论