开发者

Php and python regexp difference?

I need to parse a string 'Open URN: 100000 LA: ' and get 1开发者_JAVA百科00000 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.


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.


Except of mentioned differences I found one more. re.match(r"\s", "a b") in python with preg_match("/\s/", "a b"), the first doesn't return matches in python while the second will find space symbol. I didn't find why in official docs, it's hard to understand but it's a fact.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜