[英]Flex 3 Regular Expression Problem
I've written a url validator for a project I am working on. 我为我正在进行的项目编写了一个url验证器。 For my requirements it works great, except when the last part for the url goes longer than 22 characters it breaks. 根据我的要求,它很有效,除非url的最后一部分超过22个字符,它会中断。 My expression: 我的表情:
/((https?):\/\/)([^\s.]+.)+([^\s.]+)(:\d+\/\S+)/i
It expects input that looks like "http(s)://hostname:port/location". 它期望输入看起来像“http(s):// hostname:port / location”。 When I give it the input: 当我给它输入时:
https://demo10:443/111112222233333444445
it works, but if I pass the input 它工作,但如果我通过输入
https://demo10:443/1111122222333334444455
it breaks. 它打破了。 You can test it out easily at http://ryanswanson.com/regexp/#start . 您可以在http://ryanswanson.com/regexp/#start上轻松测试。 Oddly, I can't reproduce the problem with just the relevant (I would think) part /(:\\d+\\/\\S+)/i
. 奇怪的是,我无法用相关的(我认为)部分/(:\\d+\\/\\S+)/i
重现问题。 I can have as many characters after the required / and it works great. 在所需的/之后我可以拥有尽可能多的字符,并且效果很好。 Any ideas or known bugs? 任何想法或已知的错误?
Edit: Here is some code for a sample application that demonstrates the problem: 编辑:以下是演示此问题的示例应用程序的一些代码:
<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>
This is a bug, either in Ryan's implementation or within Flex/Flash. 这是一个错误,无论是在Ryan的实现中还是在Flex / Flash中。
The regular expression syntax used above (less surrounding slashes and flags) matches Python which provides the following output: 上面使用的正则表达式语法(较少的周围斜杠和标志)匹配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')
I debugged your regular expression on RegexBuddy and apparently it takes millions of steps to find a match. 我在RegexBuddy上调试了你的正则表达式,显然需要数百万步才能找到匹配项。 This usually means that something is terribly wrong with the regular expression. 这通常意味着正则表达式出现了严重错误。
Look at ([^\\s.]+.)+([^\\s.]+)(:\\d+\\/\\S+)
. 看([^\\s.]+.)+([^\\s.]+)(:\\d+\\/\\S+)
。
1- It seems like you're trying to match subdomains too, but it doesn't work as intended since you didn't escape the dot. 1-看起来你也试图匹配子域名,但由于你没有逃脱点,它不会按预期工作。 If you escape it, demo10:443/123 won't match because it'll need at least one dot. 如果你逃脱它,demo10:443/123将无法匹配,因为它至少需要一个点。 Change ([^\\s.]+\\.)+
to ([^\\s.]+\\.)*
and it'll work. 将([^\\s.]+\\.)+
更改为([^\\s.]+\\.)*
并且它将起作用。
2- [^\\s.]+
is a bad character class, it will match the whole string and start backtracking from there. 2- [^\\s.]+
是一个糟糕的字符类,它将匹配整个字符串并从那里开始回溯。 You can avoid this by using [^\\s:.]
which will stop at the colon. 您可以通过使用[^\\s:.]
来避免这种情况,它会停在冒号处。
This one should work as you want: https?:\\/\\/([^\\s:.]+\\.)*([^\\s:.]+):\\d+\\/\\S+
这个应该可以按你的需要工作: https?:\\/\\/([^\\s:.]+\\.)*([^\\s:.]+):\\d+\\/\\S+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.