1

I've been looking and playing with RegEx for a while now and am trying to find this solution that I can apply to both AS3 and to HTML5.

I've got a custom user entry section, 256 chars that they can customize.

What I would like is for them to use my predefined table of codes 00 - 99 and they can insert them into the box to automatically generate a response that can go through a few hundred examples.

Here is a simple example:

Please call: 04
And ask for help for product ID:
03

I'd be able to take this and say, okay i got the following into an array:

[Please call: ]
[04]
[/n]
[And ask for help for product ID: ]
[/n]
[03]

and possibly apply a flag to say whether this is a database entry or not

[Please call: ][false]
[04][true]
[/n][false]
[And ask for help for product ID: ][false]
[/n][false]
[03][true]

this would be something that my program could read. Where I know that for the ## matches, to find a database entry and insert, though for anything else, use the strings.

I have been playing around on http://gskinner.com/RegExr/

to try and brute force an answer to no avail so far.

Any help would be greatly appreciated.

The best I've come up with so far for matches is the following. Though this is my first time playing with the regex functions and would need to find out how to push these entries into my ordered array.

\d\d
\D+

And would need some way to combine them to pull an array... or I'll be stuck with a crappy loop:

//AS3 example
database_line_item:int = 127;
previous_value_was_int:boolean = false;
_display_text:string = "";
for(var i:int = 0; i < string.length; i++){
    if(string.charAt(i) is int){
        if(previous_value_was_in){
            previous_value_was_int = true;
        }else{
            _display_text += getDatabaseValue(string.charAt(i-1)+string.charAt(i), database_line_item);
            previous_value_was_int = false;
        }
    }else{
        //Hopefully this handles carriage returns, if not, will have to add that in.
        _display_text += string.charAt(i);
    }
}
// >>>>>>>>> HTML5 Example <<<<<<<<<<<<<
    ...

and I would cycle through the database_line_item, though for maybe 400 line items, this will be a taxing, to go through that string. Splitting it into smaller arrays would be easier to handle.

1 Answer 1

1

Here is the magic reg : /([^0-9\n\r]+)|(\d+)|(\r\n?|\n)/gi

Exemple output :

[Please call: ][false]
[4][true]
[/n][false]
[And ask for help for product ID:][false]
[/n][false]
[3][true]

Exemple code that do the job and put the data into an array :

package
{
    import flash.display.Sprite;

    public class TestReg extends Sprite
    {
        public function TestReg()
        {
            super();
            var data : Array = parse("Please call: 04\n"+
                        "And ask for help for product ID:\n"+
                        "03");

            // Output
            for(var i : uint = 0; i < data.length; i += 2)
                trace("[" + data[ i ] + "][" + data[ i + 1 ] + "]");
        }

        private var _search : RegExp = /([^0-9\n\r]+)|(\d+)|(\r\n?|\n)/gi;
        public function parse(str : String) : Array
        {
            var result : Array = [];
            var data : Array = str.match( _search );
            for each(var item : * in data)
            {
                // Replace new line by /n
                if(item.charAt( 0 ) == "\n" || item.charAt( 0 ) == "\r")
                    item = "/n";

                // Convert to int if is a number
                if( ! isNaN( parseFloat( item.charAt( 0 ) ) ) )
                    item = parseInt( item );

                result.push( item );
                result.push( !( item is String ));
            }

            return result;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That is awesome! I think it is almost there. What happens when I have an example of: 010203/n12 it should be able to separate and find a max of only 2 numbers. the fact that 04 changes to 4 is not an issue. Though how would I expand the RegExp to validate 2 numbers side by side?
In this case, if you want to match only two digit, just change the expr to : /([^0-9\n\r]+)|(\d{1,2})|(\r\n?|\n)/gi, where \d{1,2} mean match 1 or 2 times a digit char

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.