HaxeDoc2

(やまだぃちぅ) #1

  • |”OR” statement.


For example, the following regular expression matches valid email addresses:
1 ̃/[A-Z0-9._\%-]+@[A-Z0-9.-]+\.[A-Z][A-Z][A-Z]?/i;
Please notice that theiat the end of the regular expression is aflagthat enables case-insensitive
matching.
The possible flags are the following:


  • icase insensitive matching

  • gglobal replace or split, see below

  • mmultiline matching,ˆand$represent the beginning and end of a line

  • sthe dot.will also match newlines(Neko, C++, PHP and Java targets only)

  • uuse UTF-8 matching(Neko and C++ targets only)


10.3.1 Matching.......................................


Probably one of the most common uses for regular expressions is checking whether a string
matches the specific pattern. Thematchmethod of a regular expression object can be used to do
that:
1 class Main {
2 static function main() {
3 var r = ̃/world/;
4 var str = "hello world";
5 // true : ’world’ was found inthe string
6 trace(r.match(str));
7 trace(r.match("hello !")); //false
8 }
9 }

10.3.2 Groups........................................


Specific information can be extracted from a matched string by usinggroups. Ifmatch()returns
true, we can get groups using thematched(X)method, where X is the number of a group
defined by regular expression pattern:
1 class Main {
2 static function main() {
3 var str = "Nicolas is 26 yearsold";
4 var r =
5 ̃/([A-Za-z]+) is ([0-9]+) yearsold/;
6 r.match(str);
7 trace(r.matched(1)); // "Nicolas"
8 trace(r.matched(2)); // "26"
9 }
10 }


Note that group numbers start with 1 andr.matched(0)will always return the whole
matched substring.
Ther.matchedPos()will return the position of this substring in the original string:
Free download pdf