Changes between Version 10 and Version 11 of StdLib2/RegExp
- Timestamp:
- 07/17/09 14:38:14 (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
StdLib2/RegExp
v10 v11 13 13 These are some regexp strings which are defined for convenience. 14 14 15 '''`regexp.email`''':: 16 Matches email addresses. 15 === '''`regexp.email`''' === 17 16 18 '''`regexp.url`''':: 19 Matches HTTP URLs. 17 Matches email addresses. 20 18 21 '''`regexp.alpha`''':: 22 Matches lower- and upper-case letters and the underscore. 19 === '''`regexp.url`''' === 23 20 24 '''`regexp.space`''':: 25 Matches all whitespace characters. 21 Matches HTTP URLs. 26 22 27 '''`regexp.digit`''':: 28 Matches all decimal digits. 23 === '''`regexp.alpha`''' === 29 24 30 '''`regexp.hexdigit`''':: 31 Matches all hexadecimal digits. 25 Matches lower- and upper-case letters and the underscore. 32 26 33 '''`regexp.octdigit`''':: 34 Matches all octal digits. 27 === '''`regexp.space`''' === 35 28 36 '''`regexp.symbol`''':: 37 Matches the following symbols: `( ) [ ] . , ; = < > + - * / & ^` 29 Matches all whitespace characters. 38 30 39 '''`regexp.chinese`''':: 40 Matches all valid Chinese characters. 31 === '''`regexp.digit`''' === 41 32 42 '''`regexp.cnPhone`''':: 43 Matches valid Chinese phone numbers. 33 Matches all decimal digits. 44 34 45 '''`regexp.cnMobile`''':: 46 Matches valid Chinese mobile phone numbers. 35 === '''`regexp.hexdigit`''' === 47 36 48 '''`regexp.cnZip`''':: 49 Matches valid Chinese ZIP codes. 37 Matches all hexadecimal digits. 50 38 51 '''`regexp.cnIDcard`''':: 52 Matches valid Chinese ID card numbers. 39 === '''`regexp.octdigit`''' === 53 40 54 '''`regexp.usPhone`''':: 55 Matches valid USA phone numbers. 41 Matches all octal digits. 56 42 57 '''`regexp.usZip`''':: 58 Matches valid USA ZIP codes. 43 === '''`regexp.symbol`''' === 44 45 Matches the following symbols: `( ) [ ] . , ; = < > + - * / & ^` 46 47 === '''`regexp.chinese`''' === 48 49 Matches all valid Chinese characters. 50 51 === '''`regexp.cnPhone`''' === 52 53 Matches valid Chinese phone numbers. 54 55 === '''`regexp.cnMobile`''' === 56 57 Matches valid Chinese mobile phone numbers. 58 59 === '''`regexp.cnZip`''' === 60 61 Matches valid Chinese ZIP codes. 62 63 === '''`regexp.cnIDcard`''' === 64 65 Matches valid Chinese ID card numbers. 66 67 === '''`regexp.usPhone`''' === 68 69 Matches valid USA phone numbers. 70 71 === '''`regexp.usZip`''' === 72 73 Matches valid USA ZIP codes. 59 74 60 75 == '''class Regexp''' == 62 77 This is a class whose instances represent compiled regular expressions. 63 78 64 '''`this(pattern: string, attrs: string = "")`''':: 65 Compiles a regular expression string into an object. The `pattern` parameter is the regular expression to compile. The `attrs` parameter is a string containing extra attributes for the regexp. Currently the only implemented attribute is "g", which means for the regexp to perform globally, that is, it will find all matches of a pattern in a string instead of just the first one. 79 === '''`this(pattern: string, attrs: string = "")`''' === 66 80 67 '''`test(string: string)`''':: 68 Tests if `string` matches this pattern. Returns `true` if it matches, and `false` otherwise. If it returns `true`, the matches will be filled in, which can then be retrieved with the `match` method. For example: 81 Compiles a regular expression string into an object. The `pattern` parameter is the regular expression to compile. The `attrs` parameter is a string containing extra attributes for the regexp. Currently the only implemented attribute is "g", which means for the regexp to perform globally, that is, it will find all matches of a pattern in a string instead of just the first one. 82 83 === '''`test(string: string)`''' === 84 85 Tests if `string` matches this pattern. Returns `true` if it matches, and `false` otherwise. If it returns `true`, the matches will be filled in, which can then be retrieved with the `match` method. For example: 69 86 70 87 {{{ 76 93 }}} 77 94 78 '''`match(m: int|string = 0)`''':: 79 This method has two forms. If it is given 0, returns the current match (for use during iteration). This is the default value, so you can call it without parameters. If it is given a positive integer, returns the ''n''th parenthesized subexpression in the pattern. If it is given a string, it looks for matches to this pattern in `string`, and returns an array of all the strings representing the matches. 95 === '''`match(m: int|string = 0)`''' === 80 96 81 '''`find(string: string)`''':: 82 Looks for this pattern in `string`. Returns the 0-based position if found, and the length of the string otherwise. 97 This method has two forms. If it is given 0, returns the current match (for use during iteration). This is the default value, so you can call it without parameters. If it is given a positive integer, returns the ''n''th parenthesized subexpression in the pattern. If it is given a string, it looks for matches to this pattern in `string`, and returns an array of all the strings representing the matches. 83 98 84 '''`split(string: string)`''':: 85 Splits `string` using this pattern as the delimiter. Returns an array of the split-up string's components. 99 === '''`find(string: string)`''' === 86 100 87 '''`replace(string: string, replacement: string|function)`''':: 88 If `replacement` is a string, replaces any matches in `string` with the replacement format string, returning the new string. If `replacement` is a function, it will call that function with an instance of the Regexp class for each match, and will expect that function to return a replacement string. This allows for very flexible replacements. This is a small example: 101 Looks for this pattern in `string`. Returns the 0-based position if found, and the length of the string otherwise. 102 103 === '''`split(string: string)`''' === 104 105 Splits `string` using this pattern as the delimiter. Returns an array of the split-up string's components. 106 107 === '''`replace(string: string, replacement: string|function)`''' === 108 109 If `replacement` is a string, replaces any matches in `string` with the replacement format string, returning the new string. If `replacement` is a function, it will call that function with an instance of the Regexp class for each match, and will expect that function to return a replacement string. This allows for very flexible replacements. This is a small example: 89 110 90 111 {{{ 103 124 Returns the completed string. 104 125 105 '''`search(string: string)`''':: 106 This sets up the regex to start searching through the given `string`. Immediately after calling this function, you should run a `foreach` loop on the object. This is explained in `opApply`. 126 === '''`search(string: string)`''' === 107 127 108 '''`opApply()`''':: 109 Once the regex has been set up to search a string with `search`, use this to iterate over the matches with a `foreach` loop. This returns two indices for each iteration: the index of the current match (from 0), and the instance itself. From the instance, you can obtain the value of the current match by calling `.match(0)` (or just `.match()`) on it, as well as using the `.pre()` and `.post()` methods. Here's an example of use: 128 This sets up the regex to start searching through the given `string`. Immediately after calling this function, you should run a `foreach` loop on the object. This is explained in `opApply`. 129 130 === '''`opApply()`''' === 131 132 Once the regex has been set up to search a string with `search`, use this to iterate over the matches with a `foreach` loop. This returns two indices for each iteration: the index of the current match (from 0), and the instance itself. From the instance, you can obtain the value of the current match by calling `.match(0)` (or just `.match()`) on it, as well as using the `.pre()` and `.post()` methods. Here's an example of use: 110 133 111 134 {{{ 115 138 }}} 116 139 117 '''`pre()`''':: 118 During iteration, returns the portion of the source string that comes before the current match. 140 === '''`pre()`''' === 119 141 120 '''`post()`''':: 121 During iteration, returns the portion of the source string that comes after the current match. 142 During iteration, returns the portion of the source string that comes before the current match. 143 144 === '''`post()`''' === 145 146 During iteration, returns the portion of the source string that comes after the current match