AIR, Flex - how to check if regex is valid
i want to check in Adobe AIR if given regex is valid. I'm looking for somtehing similar like here: How to check if a given Regex is valid?
I dont want to compare regex and text-value - i jus want to check if this regex is valid. If someone type invalid regex - for example: "x{5,-3}" or "(((^^$$$)//)" or something like this i just need to communicate to him that this regular expression is not valid - its not proper regular expression.
In Java it can be done by: [code]
try {
Pattern.compile(userInputPattern);
} catch (PatternSyntaxException exception) {
System.err.println(exception.getDescription());
System.exit(1);
}
开发者_C百科
[/code]
As far as I can tell, you are looking for a test app in which you can enter an regular expression and a value and the app will tell you if there is a match or not. Assuming that is what you want, this code will do it for you:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
private function test(regex:String, value:String):String {
return new RegExp(regex).test(value) ? "MATCH" : "NOT A MATCH";
}
]]>
</fx:Script>
<s:Form>
<s:FormItem label="RegEx:">
<s:TextInput id="regex" />
</s:FormItem>
<s:FormItem label="Test Value: ">
<s:TextInput id="testValue" />
</s:FormItem>
<s:Label text="{test(regex.text, testValue.text)}" />
</s:Form>
</s:WindowedApplication>
If you want to dynamicaly see the result of your regexp on a given input, I suggest you this online tool:
Flex 3 Regular Expression Explorer
What do you mean by "valid"? If you want to check if the regular expression matches a particular string, you can use RegExp::test()
- it will return true
if there is a match, false
if there isn't.
I can't find this implemented in AS3, but here there is the source code Pattern.java
https://github.com/unkiwii/Random-Stuff/blob/master/java/src/java/util/regex/Pattern.java
Maybe you can implement the same in AS3.
精彩评论