3

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

The following 2 regular expressions work in Python:

'/\*\*([\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?

1
  • 2
    You have to use proper delimiters php.net/manual/en/regexp.reference.delimiters.php. The first character is always treated as delimiter. So PHP interprets the first expression only until <>=:/ and then finds the ] which is not a valid modifier. Commented Aug 7, 2011 at 15:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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+)(?: (.+))?)%

1 Comment

Ha -- got it! I was using '/', but it's actually used inside the pattern!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.