Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }

[/code]

like image 384
mihau Avatar asked Nov 05 '22 15:11

mihau


2 Answers

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>
like image 176
Brian Genisio Avatar answered Nov 07 '22 13:11

Brian Genisio


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

like image 32
Davz Avatar answered Nov 07 '22 12:11

Davz