[英]Flex 3 Regular Expression Problem
我为我正在进行的项目编写了一个url验证器。 根据我的要求,它很有效,除非url的最后一部分超过22个字符,它会中断。 我的表情:
/((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)/i
它期望输入看起来像“http(s):// hostname:port / location”。 当我给它输入时:
https://demo10:443/111112222233333444445
它工作,但如果我通过输入
https://demo10:443/1111122222333334444455
它打破了。 您可以在http://ryanswanson.com/regexp/#start上轻松测试。 奇怪的是,我无法用相关的(我认为)部分/(:\\d+\\/\\S+)/i
重现问题。 在所需的/之后我可以拥有尽可能多的字符,并且效果很好。 任何想法或已知的错误?
编辑:以下是演示此问题的示例应用程序的一些代码:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private function click():void {
var value:String = input.text;
var matches:Array = value.match(/((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)/i);
if(matches == null || matches.length < 1 || matches[0] != value) {
area.text = "No Match";
}
else {
area.text = "Match!!!";
}
}
]]>
</mx:Script>
<mx:TextInput x="10" y="10" id="input"/>
<mx:Button x="178" y="10" label="Button" click="click()"/>
<mx:TextArea x="10" y="40" width="233" height="101" id="area"/>
</mx:Application>
这是一个错误,无论是在Ryan的实现中还是在Flex / Flash中。
上面使用的正则表达式语法(较少的周围斜杠和标志)匹配Python,它提供以下输出:
# ignore case insensitive flag as it doesn't matter in this case
>>> import re
>>> rx = re.compile('((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)')
>>> print rx.match('https://demo10:443/1111122222333334444455').groups()
('https://', 'https', 'demo1', '0', ':443/1111122222333334444455')
我在RegexBuddy上调试了你的正则表达式,显然需要数百万步才能找到匹配项。 这通常意味着正则表达式出现了严重错误。
看([^\\s.]+.)+([^\\s.]+)(:\\d+\\/\\S+)
。
1-看起来你也试图匹配子域名,但由于你没有逃脱点,它不会按预期工作。 如果你逃脱它,demo10:443/123将无法匹配,因为它至少需要一个点。 将([^\\s.]+\\.)+
更改为([^\\s.]+\\.)*
并且它将起作用。
2- [^\\s.]+
是一个糟糕的字符类,它将匹配整个字符串并从那里开始回溯。 您可以通过使用[^\\s:.]
来避免这种情况,它会停在冒号处。
这个应该可以按你的需要工作: https?:\\/\\/([^\\s:.]+\\.)*([^\\s:.]+):\\d+\\/\\S+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.