doc/cross/regexp
Differences between version EMPTY and #1662
0a1,91
> Haxe supporte nativement les expression régulières. Elles peuvent être utilisées pour vérifier le format d'une chaine de caractères ou pour extraire des données régulières d'un texte. Une expression régulière débute avec ''~/'' et se termine avec un seul ''/'' :
>
> <code haxe>
> var r : EReg = ~/world/;
> var str = "hello world";
> trace(r.match(str)); // true : 'world' a été trouvé dans la chaine
> trace(r.match("hello !")); // false
> </code>
>
> Vous pouvez utiliser des motifs (patterns) d'expression régulières comme (mais pas uniquement) :
> * ''.'' : n'importe quel caractère
> * ''*'' : répéter 0 ou plusieurs fois
> * ''+'' : répéter une fois ou plus
> * ''?'' : optionnel (non présent ou présent une fois)
> * ''[A-Z0-9]'' : ensemble de caractères
> * ''[^\r\n\t]'' : caractère non-présent dans l'ensemble de caractères
> * ''(...)'' : parenthesis to match groups of characters
> * ''^'' : beginning of string/line
> * ''$'' : end of string/line
>
> For example, the following regular expression match a valid email address :
>
> <code haxe>
> ~/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z][A-Z][A-Z]?/i;
> </code>
>
> Please notice that the ''i'' at the end of the regular expression is a //Flag// that enable case-insensitive matching.
>
> The possible flags are the following :
> * ''i'' : case insensitive matching
> * ''g'' : global replace or split, see below
> * ''m'' : multiline matching, ''^'' and ''$'' represent only the beginning and end of the string
> * ''s'' : the dot ''.'' will match also newlines //(Haxe/Server only)//
>
> ====== Groups ======
>
> You can extract some informations by using groups :
>
> <code haxe>
> var str = "Nicolas is 26 years old";
> var r = ~/([A-Za-z]+) is ([0-9]+) years old/;
> r.match(str);
> trace(r.matched(1)); // "Nicolas"
> trace(r.matched(2)); // "26"
> </code>
>
> The ''r.matched(0)'' result will always return the whole matched substring, and ''r.matchedPos()'' will return the position of this substring in the original string :
>
> <code haxe>
> var str = "abcdeeeeefghi";
> var r = ~/e*/;
> r.match(str);
> trace(r.matched(0)); // "eeeee"
> trace(r.matchedPos()); // { pos : 4, len : 5 }
> </code>
>
> ====== Replace ======
>
> A regular expression can also be used to replace a part of the string :
>
> <code haxe>
> var str = "aaabcbcbcbz";
> var r = ~/b[^c]/g; // g : replace all instances
> trace(r.replace(str,"xx")); // "aaaxxxxxxbz"
> </code>
>
> You can use ''$X'' to reuse a matched group in the replacement :
>
> <code haxe>
> var str = "{hello} {0} {again}";
> var r = ~/{([a-z]+)}/g;
> trace(r.replace(str,"*$1*")); // "*hello* {0} *again*"
> </code>
>
> ====== Split ======
>
> A regular expression can also be used to split a string into several substrings. In that case, the delimiter used to split is not a constant string but a regular expression :
>
> <code haxe>
> var str = "XaaaYababZbbbW";
> var r = ~/[ab]+/g;
> trace(r.split(str)); // ["X","Y","Z","W"]
> </code>
>
> ====== Implementation Details ======
>
> Regular Expressions are implemented :
> * in Javascript, the Browser is providing the implementation with the object RegExp.
> * in Neko, the PCRE library is used
> * in Flash9, the native implementation is used
> * FIXME in Flash 6/8, the implementation is not yet available but will a pure Haxe version (hence very since not native slow, but compatible)
| Ver | Date | Lg | User | Action |
|---|---|---|---|---|
| #17988 | 2013-03-23 05:48:26 | jp | TheCoolMuseum | View | Diff |
| #17987 | 2013-03-23 05:39:18 | jp | TheCoolMuseum | View | Diff |
| #17986 | 2013-03-23 05:39:18 | jp | TheCoolMuseum | Set title to 正規表現 |
| #15783 | 2012-12-10 02:41:50 | en | TheHippo | View | Diff |
| #13980 | 2012-05-14 01:52:59 | en | waneck | View | Diff |
| #13979 | 2012-05-13 19:50:55 | en | waneck | View | Diff |
| #13978 | 2012-05-13 19:50:34 | en | waneck | View | Diff |
| #13973 | 2012-05-11 10:33:16 | en | bubblebenj | View | Diff |
| #13759 | 2012-04-15 08:48:28 | en | elyon | View | Diff |
| #12595 | 2012-02-23 17:30:46 | en | paletz | View | Diff |
| #10829 | 2011-08-11 10:19:49 | ru | dmpost | View | Diff |
| #10828 | 2011-08-11 10:17:19 | en | dmpost | View | Diff |
| #10827 | 2011-08-11 10:16:42 | en | dmpost | View | Diff |
| #10221 | 2011-02-25 21:57:36 | ru | antony | View | Diff |
| #10220 | 2011-02-25 21:57:36 | ru | antony | Set title to Использование регулярных выражений |
| #9408 | 2010-12-03 14:09:54 | en | ncannasse | View | Diff |
| #6884 | 2009-08-24 17:20:05 | fr | Korko | View | Diff |
| #6245 | 2009-06-27 09:50:28 | en | theom | View | Diff |
| #6244 | 2009-06-27 09:48:43 | en | theom | View | Diff |
| #1666 | 2008-06-21 21:56:36 | fr | bdasnois | View | Diff |
Previous | Next