English 中文(简体)
符合优先优先级怪异的正则表达式
原标题:Regular expressions matching priority weirdness
  • 时间:2012-05-23 19:47:45
  •  标签:
  • php
  • regex

在php中写入一个插曲脚本时,我遇到了一个很奇怪的行为(至少对我来说是这样 ) 。 我的正则表达式包含许多不同的文字结尾模式, 似乎符合第二个可接受的选项, 即使第一个选项完全可以。

整个表达方式是这样的(你真的不需要注意这一点):

([kpt])1([aou])$|(?:(n)t)?([auo])$|([aeou][^aeiouyäö]+[aeiou])$|([auo][^aeiouyäö]+)$|([^aeiouyäö])(?!6)([^aeiouyäö])(e)$|((?:[auo]i|[auo])[^aeiouyäö]*)([aeiouyäö])10$

毋庸置疑,我首先怀疑我犯了一个错误, 于是我轻轻地哑了这番话, 终于走到了这个阶段:

([aeiou])$|(.+)$

字面意思应该是“在字符串结尾处匹配一个元音,然后作为后置引用 1 或如果它失败了,则在字符串结尾处匹配任何字符的任意次数,然后将其作为后置引用 2 ”

当用于像 foo Id 这样的字符串时, 期望表达式的第一部分( ([aeiou])$ ) 将被使用。 而第二个则被使用, 这让我困惑 。

如果删除了该量化符, 则使用第一个选项。 我猜这与表达式部件的贪婪或特殊性有关, 虽然我认为表达式是左向右测试 。

谁能解释一下这个行为吗?

最佳回答

在字符串结尾处匹配一个元音符 [.] 或“ 坚固” 如果失败 , 在字符串结尾处匹配任意字符 。

不,这不是它的意思,正确的解释是:

匹配 < 强度 > 长于 :

  • one vowel at the end of the string
  • any character any number of times before end of the string

(我不知道是否明确规定了断领规则。 )

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签