EReg

class ERegAvailable in flash8, flash, neko, js, php, cpp, cs, javaRegular expression is a way to find regular patterns into Strings.
Have a look a Using Regular Expressions to learn how to use them.
EReg example: Email Validation (cross platform)
function new( r : String, opt : String ) : VoidAvailable in cs, javaCreates a new regular expression with pattern r and options opt. function new( r : String, opt : String ) : VoidAvailable in flash8, flash, neko, js, php, cppCreates a new regular expression with pattern r and options opt. function map( s : String, f : EReg -> String ) : StringAvailable in cs, java function map( s : String, f : EReg -> String ) : StringAvailable in flash8, flash, neko, js, php, cpp function match( s : String ) : BoolAvailable in cs, javaTells if the regular expression matches the String. Updates the internal state accordingly. function match( s : String ) : BoolAvailable in flash8, flash, neko, js, php, cppTells if the regular expression matches the String. Updates the internal state accordingly. function matchSub( s : String, pos : Int, ?len : Int ) : BoolAvailable in cs, java function matchSub( s : String, pos : Int, ?len : Int ) : BoolAvailable in flash8, flash, neko, js, php, cpp function matched( n : Int ) : StringAvailable in cs, javaReturns a matched group or throw an exception if there is no such group. If n = 0, the whole matched substring is returned. function matched( n : Int ) : StringAvailable in flash8, flash, neko, js, php, cppReturns a matched group or throw an exception if there is no such group. If n = 0, the whole matched substring is returned. function matchedLeft() : StringAvailable in cs, javaReturns the part of the string to the left of the matched substring. function matchedLeft() : StringAvailable in flash8, flash, neko, js, php, cppReturns the part of the string to the left of the matched substring. function matchedPos() : { pos : Int, len : Int }Available in cs, javaReturns the position of the matched substring within the original matched string. function matchedPos() : { pos : Int, len : Int }Available in flash8, flash, neko, js, php, cppReturns the position of the matched substring within the original matched string. function matchedRight() : StringAvailable in cs, javaReturns the part of the string to the right of of the matched substring. function matchedRight() : StringAvailable in flash8, flash, neko, js, php, cppReturns the part of the string to the right of of the matched substring. function replace( s : String, by : String ) : StringAvailable in cs, javaReplaces a pattern by another string. The by format can contains $1 to $9 that will correspond to groups matched while replacing. $$ means the $ character. function replace( s : String, by : String ) : StringAvailable in flash8, flash, neko, js, php, cppReplaces a pattern by another string. The by format can contains $1 to $9 that will correspond to groups matched while replacing. $$ means the $ character. function split( s : String ) : Array<String>Available in cs, javaSplit a string by using the regular expression to match the separators. function split( s : String ) : Array<String>Available in flash8, flash, neko, js, php, cppSplit a string by using the regular expression to match the separators.
version #19308, modified 2013-05-08 11:13:10 by api
3 comments
  • May 01, 2013 at 01:01

    matchedPos throws an exception if the EReg hasn't yet been executed on a string

  • Dec 03, 2013 at 22:19

    How to iterate through a list of matched groups, for example:

    var data:String = "1 AKQJT\n2 KQJT9\n3 QJT98\n4 JT987\n5 T9876 ...";
    var filter:EReg = ~/(\d*) ([2-9TJQKA]{5})/g;
    filter.match(data);
    trace(filter.matched(1), filter.matched(2));

    prints "1,AKQJT" and this is correct, but it's just first iteration. I'd like to dee full list like "1,AKQJT", "2,KQJT9" and so on.

    ActionScript uses slightly different paradigm and allows to iterate through matched groups list like:

    var data:String = "1 AKQJT\n2 KQJT9\n3 QJT98\n4 JT987\n5 T9876 ...";
    var filter:RegExp = /(\d*) ([2-9TJQKA]{5})/g;
    var result:Object = filter.exec(data);
    while (result) {
        trace(result[1], result[2]);
        result = filter.exec(data); // goto next found groups or return null if data exceeded
    }

  • Dec 03, 2013 at 22:33

    I have found solution:

    var data:String = "1 AKQJT\n2 KQJT9\n3 QJT98\n4 JT987\n5 T9876 ...";
    var filter:EReg = ~/(\d*) ([2-9TJQKA]{5})/g;
    while (data != null) {
        if (!filter.match(data))
            break;
        data = filter.matchedRight();
        trace(filter.matched(1), filter.matched(2));
    }