tags:

views:

62

answers:

1

I need to parse a string 'Open URN: 100000 LA: ' and get 100000 from it. on python regexp (?<=Open URN: )[0-9]+(?= LA:) works fine but in php it gives following error:

preg_match(): Unknown modifier '['

I need it working php, so please help me to solve this problem and tell about difference in python and php regexps.

+3  A: 

You have to use delimiters when you are using the Perl Compatible Regular Expressions (PCRE) functions in PHP (to which preg_match() belongs).

From the documentation:

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

The reason for using delimiters is that you can add pattern modifiers after the last delimiter, e.g. to make an case-insensitive match:

#[a-z]#i  // # is the delimiter.

Back to your problem:

In your case, PHP thinks the brackets () are your delimiters (yes, opening and closing brackets are valid delimiters, see the documentation) and ?<=Open URN: is your pattern . Then it encounters [ and treats it as pattern modifier, but it is not a valid one.

Your pattern with delimiter %:

preg_match('%(?<=Open URN: )[0-9]+(?= LA:)%', 'Open URN: 100000 LA: ');

There are a lot examples in the documentation of preg_match()


Python vs PHP

The only thing I found regarding regular expressions in Python is, that Perl syntax is used but I don't know if the full syntax is supported.
As already mentioned, PHP uses PCRE. Description of the differences between PCRE and Perl regex.

Felix Kling
[php docs about delimiters](http://www.php.net/manual/en/regexp.reference.delimiters.php)
SilentGhost