|
| 1 | +# Regex |
| 2 | + |
| 3 | +Built-in library for regular expressions |
| 4 | + |
| 5 | +#### Example usage |
| 6 | + |
| 7 | +```lua |
| 8 | +local Regex = require("@lune/regex") |
| 9 | + |
| 10 | +local re = Regex.new("hello") |
| 11 | + |
| 12 | +if re:isMatch("hello, world!") then |
| 13 | + print("Matched!") |
| 14 | +end |
| 15 | + |
| 16 | +local caps = re:captures("hello, world! hello, again!") |
| 17 | + |
| 18 | +print(#caps) -- 2 |
| 19 | +print(caps:get(1)) -- "hello" |
| 20 | +print(caps:get(2)) -- "hello" |
| 21 | +print(caps:get(3)) -- nil |
| 22 | +``` |
| 23 | + |
| 24 | +## Constructors |
| 25 | + |
| 26 | +### new |
| 27 | + |
| 28 | +Creates a new `Regex` from a given string pattern. |
| 29 | + |
| 30 | +#### Errors |
| 31 | + |
| 32 | +This constructor throws an error if the given pattern is invalid. |
| 33 | + |
| 34 | +#### Parameters |
| 35 | + |
| 36 | +- `pattern` `string` The string pattern to use |
| 37 | + |
| 38 | +#### Returns |
| 39 | + |
| 40 | +- `Regex` The new Regex object |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Methods |
| 45 | + |
| 46 | +### isMatch |
| 47 | + |
| 48 | +Check if the given text matches the regular expression. |
| 49 | + |
| 50 | +This method may be slightly more efficient than calling `find` if you only need to know if the text |
| 51 | +matches the pattern. |
| 52 | + |
| 53 | +#### Parameters |
| 54 | + |
| 55 | +- `self` Regex |
| 56 | + |
| 57 | +- `text` `string` The text to search |
| 58 | + |
| 59 | +#### Returns |
| 60 | + |
| 61 | +- `boolean` Whether the text matches the pattern |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### find |
| 66 | + |
| 67 | +Finds the first match in the given text. |
| 68 | + |
| 69 | +Returns `nil` if no match was found. |
| 70 | + |
| 71 | +#### Parameters |
| 72 | + |
| 73 | +- `self` Regex |
| 74 | + |
| 75 | +- `text` `string` The text to search |
| 76 | + |
| 77 | +#### Returns |
| 78 | + |
| 79 | +- `RegexMatch?` The match object |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### captures |
| 84 | + |
| 85 | +Finds all matches in the given text as a `RegexCaptures` object. |
| 86 | + |
| 87 | +Returns `nil` if no matches are found. |
| 88 | + |
| 89 | +#### Parameters |
| 90 | + |
| 91 | +- `self` Regex |
| 92 | + |
| 93 | +- `text` `string` The text to search |
| 94 | + |
| 95 | +#### Returns |
| 96 | + |
| 97 | +- `RegexCaptures?` The captures object |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +### split |
| 102 | + |
| 103 | +Splits the given text using the regular expression. |
| 104 | + |
| 105 | +#### Parameters |
| 106 | + |
| 107 | +- `self` Regex |
| 108 | + |
| 109 | +- `text` `string` The text to split |
| 110 | + |
| 111 | +#### Returns |
| 112 | + |
| 113 | +- `{ string }` The split text |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +### replace |
| 118 | + |
| 119 | +Replaces the first match in the given text with the given replacer string. |
| 120 | + |
| 121 | +#### Parameters |
| 122 | + |
| 123 | +- `self` Regex |
| 124 | + |
| 125 | +- `haystack` `string` The text to search |
| 126 | + |
| 127 | +- `replacer` `string` The string to replace matches with |
| 128 | + |
| 129 | +#### Returns |
| 130 | + |
| 131 | +- `string` The text with the first match replaced |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +### replaceAll |
| 136 | + |
| 137 | +Replaces all matches in the given text with the given replacer string. |
| 138 | + |
| 139 | +#### Parameters |
| 140 | + |
| 141 | +- `self` Regex |
| 142 | + |
| 143 | +- `haystack` `string` The text to search |
| 144 | + |
| 145 | +- `replacer` `string` The string to replace matches with |
| 146 | + |
| 147 | +#### Returns |
| 148 | + |
| 149 | +- `string` The text with all matches replaced |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +# RegexCaptures |
| 154 | + |
| 155 | +Captures from a regular expression. |
| 156 | + |
| 157 | +## Methods |
| 158 | + |
| 159 | +### get |
| 160 | + |
| 161 | +Returns the match at the given index, if one exists. |
| 162 | + |
| 163 | +#### Parameters |
| 164 | + |
| 165 | +- `self` RegexCaptures |
| 166 | + |
| 167 | +- `index` `number` The index of the match to get |
| 168 | + |
| 169 | +#### Returns |
| 170 | + |
| 171 | +- `RegexMatch` The match, if one exists |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +### group |
| 176 | + |
| 177 | +Returns the match for the given named match group, if one exists. |
| 178 | + |
| 179 | +#### Parameters |
| 180 | + |
| 181 | +- `self` RegexCaptures |
| 182 | + |
| 183 | +- `group` `string` The name of the group to get |
| 184 | + |
| 185 | +#### Returns |
| 186 | + |
| 187 | +- `RegexMatch` The match, if one exists |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +### format |
| 192 | + |
| 193 | +Formats the captures using the given format string. |
| 194 | + |
| 195 | +#### Example usage |
| 196 | + |
| 197 | +```lua |
| 198 | +local regex = require("@lune/regex") |
| 199 | + |
| 200 | +local re = regex.new("(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})") |
| 201 | + |
| 202 | +local caps = re:captures("On 14-03-2010, I became a Tenneessee lamb."); |
| 203 | +assert(caps ~= nil, "Example pattern should match example text") |
| 204 | + |
| 205 | +local formatted = caps:format("year=$year, month=$month, day=$day") |
| 206 | +print(formatted) -- "year=2010, month=03, day=14" |
| 207 | +``` |
| 208 | + |
| 209 | +#### Parameters |
| 210 | + |
| 211 | +- `self` RegexCaptures |
| 212 | + |
| 213 | +- `format` `string` The format string to use |
| 214 | + |
| 215 | +#### Returns |
| 216 | + |
| 217 | +- `string` The formatted string |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +# RegexMatch |
| 222 | + |
| 223 | +A match from a regular expression. |
| 224 | + |
| 225 | +Contains the following values: |
| 226 | + |
| 227 | +- `start` -- The start index of the match in the original string. |
| 228 | +- `finish` -- The end index of the match in the original string. |
| 229 | +- `text` -- The text that was matched. |
| 230 | +- `len` -- The length of the text that was matched. |
0 commit comments