HaxeDoc2

(やまだぃちぅ) #1

1 class Main {
2 static function main() {
3 var str = "abcdeeeeefghi";
4 var r = ̃/e+/;
5 r.match(str);
6 trace(r.matched(0)); // "eeeee"
7 // { pos : 4, len : 5 }
8 trace(r.matchedPos());
9 }
10 }


Additionally,r.matchedLeft()andr.matchedRight()can be used to get substrings to
the left and to the right of the matched substring:
1 class Main {
2 static function main() {
3 var r = ̃/b/;
4 r.match("abc");
5 trace(r.matchedLeft()); // a
6 trace(r.matched(0)); // b
7 trace(r.matchedRight()); // c
8 }
9 }

10.3.3 Replace........................................


A regular expression can also be used to replace a part of the string:
1 class Main {
2 static function main() {
3 var str = "aaabcbcbcbz";
4 // g : replace all instances
5 var r = ̃/b[ˆc]/g;
6 // "aaabcbcbcxx"
7 trace(r.replace(str,"xx"));
8 }
9 }
We can use$Xto reuse a matched group in the replacement:
1 class Main {
2 static function main() {
3 var str = "{hello} {0} {again}";
4 var r = ̃/{([a-z]+)}/g;
5 // "*hello* {0} *again*"
6 trace(r.replace(str,"*$1*"));
7 }
8 }

10.3.4 Split


A regular expression can also be used to split a string into several substrings:
Free download pdf