RegExpValidator不会正确验证URL模式 [英] RegExpValidator does not validate a URL pattern correctly
问题描述
http://www.ftd.de/rss2 在检查时无效(unadventurous)< mx:RegExValidator>
:
source ={inputURL}property =texttriggerEvent =focusOut
expression = {resourceManager.getString('resources','general.urlRegExp')}
flags =i/>
只要这些RegExValidator不以一个数字。任何其他有效的网址似乎是有效的。
为什么? Flex错误?
RegEx
我使用此RegEx 通过 Diego Perini ,在比较中获得最佳结果。
^(?:( ?: https?| ftp):\ / \ /)(?: \S +(?:: \S *)? @)?(?!:( 10(?:?\.\d {1,3}){3})(?127(?:\.\d {1,3}){3 })(?!169\.254(?:\.\d {1,3}){2})(?!192\.168(?:\.\d {1,3 }){2})(?172\(?: 1 [6-9] |。2\d | 3 [0-1])(?: \.\d {1,3}) {2})(?:?。[1-9] \d | 1\d\d | 2 [01] \d | 22 [0-3])(?: \(?: 1 ?\d {1,2} | 2 [0-4] \d | 25 [0-5])){2}(?: \(:?[1-9] \d | 1\d\d | 2 [0-4] \d | 25 [0-4]))|(:???(:[az\\\¡-\\\0-9] + - )* [az\\\¡-\\\0-9] +)(?: \(:?[az\\\¡-\\\0-9] + - )* [az\\\¡-\\\0- 9] +)*(?: \(:?[az\\\¡- \\\]]))(?:: \d {2,5})?(?: \ / [^ \s] *)?$
正则表达式本身对于上面的URL很有效,它在我迄今为止测试过的任何非flex验证器中都能正确验证:
<我已经用两个基于Flex的验证器检查了RegEx,它们都未能验证任何 URL:
- http://gskinner.com/RegExr/
- http:
我们使用Flex SDK 4.6.0.23201。
解决方案nhahtdh的回答让我再次检查这个问题。这并没有解决问题,但它指出了我正确的方向。我将从他的回答中使用RegExp,作为从
\uhhhh
到\x {hhhh}
是必要的。
编辑:解决方案
实际的问题是,当传递一个RegExp作为字符串到
RegExpValidator
,它必须是双重转义。因此,如果从资源文件加载模式,模式应该看起来像这样:general.urlRegExp = ^(?:(? :HTTPS | FTP):\\ / \\ /)(?: \\S +(:: \\S *)@)(:( ?! 10(????? :\\.\\d {1,3}){3})(?127(?:\\.\\d {1,3}){3})(? !169\\.254(?:\\.\\d {1,3}){2})(?!192\\.168(?:\\.\\ \\\d {1,3}){2})(?172\\(?: 1 [6-9] |。2\\d | 3 [0-1])(?: \\.\\d {1,3}){2})(?:?[1-9] \\d | 1\\d\\d | 2 [01 ] \\d | 22 [0-3])(?: \\(?: 1 \\d {1,2} |。Λ2 [0-4] \\d | 25 [0-5])){2}(?: \\(:?[1-9] \\d | 1\\d\\d | 2 [O- 4] \\d | 25 [0-4]))|(:???(:[az\\x {00A1} -\\x {FFFF} 0-9] + - ) * [{az\\x} 00A1 {-\\x FFFF} 0-9] +)(?: \\(:?[az\\x {00A1} -\ \x {FFFF} 0-9] + - )* [{az\\x} 00A1 {-\\x FFFF} 0-9] +)*(?: \\\ 。(?:[az\\x {00A1} -\\x {FFFF}] {2})?))(:: \\d {2,5-})(?: \\ / [^ \\] *)?$
现在可以直接用于
RegExpValidator
:< mx:RegExpValidator expression = {resourceManager.getString('resources','general.urlRegExp')}flags =gmi/>
The URL http://www.ftd.de/rss2 is not valid when I check it against the below RegEx in this (unadventurous)
<mx:RegExValidator>
:<mx:RegExpValidator id="validatorURL" required="true" enabled="true" source="{inputURL}" property="text" triggerEvent="focusOut" expression="{resourceManager.getString('resources','general.urlRegExp')}" flags="i" />
This RegExValidator with the below RegEx validates URLs correctly as long as they don't end with a number. Any other valid URL seems to be valid.
Why is that? Flex bug?
RegEx
I use this RegEx by Diego Perini, which gets the best results in this comparison.
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
The RegEx itself works great with the above URL, it validates correctly in any non-flex validator i've tested so far:
I've checked the RegEx with two Flex-based validators, which both failed to validate any URL at all:
We use Flex SDK 4.6.0.23201.
解决方案nhahtdh's answer made me check this issue again. It didn't solve the problem, but it pointed me in the right direction. I'll use the RegExp from his answer, as the suggestion conversion from
\uhhhh
to\x{hhhh}
is necessary as well.Edit: solution
The actual problem is that when passing a RegExp as String to a
RegExpValidator
it has to be double escaped. So the Pattern should look like this if it is loaded from a resources file:general.urlRegExp=^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]+-?)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?$
Now this can be used directly in the
RegExpValidator
:<mx:RegExpValidator expression="{resourceManager.getString('resources','general.urlRegExp')}" flags="gmi" />
这篇关于RegExpValidator不会正确验证URL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!