Repository navigation
dos_strregexp
Parses a string using a regular expression. A regular expression provides a concise and flexible means for "matching" (specifying and recognizing) strings of text, such as particular characters, words, or patterns of characters.
(dos_strregexp regexp input [ignore_case [value [replace_all]]])
regexp
The regular expression. The metacharacters understood by this function are::
| Metacharacter | Description |
|---|---|
| . | Matches any single character. |
| [ ] | Indicates a character class. Matches any character inside the brackets (for example, [abc] matches "a", "b", and "c"). |
| ^ | If this metacharacter occurs at the start of a character class, it negates the character class. A negated character class matches any character except those inside the brackets (for example, [^abc] matches all characters except "a", "b", and "c"). If ^ is at the beginning of the regular expression, it matches the beginning of the input (for example, ^[abc] will only match input that begins with "a", "b", or "c"). |
| - | In a character class, indicates a range of characters (for example, [0-9] matches any of the digits "0" through "9"). |
| ? | Indicates that the preceding expression is optional: it matches once or not at all (for example, [0-9][0-9]? matches "2" and "12"). |
| + | Indicates that the preceding expression matches one or more times (for example, [0-9]+ matches "1", "13", "666", and so on). |
| * | Indicates that the preceding expression matches zero or more times. |
| ??, +?, *? | Non-greedy versions of ?, +, and . These match as little as possible, unlike the greedy versions which match as much as possible. Example: given the input "", <.?> matches "" while <.*> matches "". |
| ( ) | Grouping operator. Example: (\d+,)*\d+ matches a list of numbers separated by commas (such as "1" or "1,23,456"). |
| { } | Indicates a match group. |
| \ | Escape character: interpret the next character literally (for example, [0-9]+ matches one or more digits, but [0-9]+ matches a digit followed by a plus character). Also used for abbreviations (such as \a for any alphanumeric character; see table below). If \ is followed by a number n, it matches the nth match group (starting from 0). Example: <{.?}>.?</\0> matches "Contents". Note, with string literals, two backslashes must be used: "\+", "\a", "<{.?}>.?</\0>". |
| $ | At the end of a regular expression, this character matches the end of the input. Example: [0-9]$ matches a digit at the end of the input. |
| | | Alternation operator: separates two expressions, exactly one of which matches (for example, T|the matches "The" or "the"). |
| ! | Negation operator: the expression following ! does not match the input. Example: a!b matches "a" not followed by "b". |
A regular expression can also contain abbreviations, or shortcuts, such as \d instead of [0-9]. The predefined character traits classes provide the following abbreviations are:
| Abbreviation | Description |
|---|---|
| \a | Any alphanumeric character: ([a-zA-Z0-9]) |
| \b | White space (blank): ([ \t]) |
| \c | Any alphabetic character: ([a-zA-Z]) |
| \d | Any decimal digit: ([0-9]) |
| \h | Any hexadecimal digit: ([0-9a-fA-F]) |
| \n | Newline: (\r|(\r?\n)) |
| \q | A quoted string: ("[^"]")|('[^']') |
| \w | A simple word: ([a-zA-Z]+) |
| \z | An integer: ([0-9]+) |
Examples of regular expressions are as follows:
| String to find | Regular expression |
|---|---|
| A date in yyyy-mm-dd format that is between 1900-01-01 and 2099-12-31 | ((19)|(20))\d\d((0[1-9])|(1[012]))((0[1-9])|([12][0-9])|(3[01])) |
| Parts of a line before and after a person's name | ^.Dale.$ |
| A string field that contains only alphabetic characters | [A-Za-z]* |
input
The input string to parse using the regular expression.
ignore_case
Specify T to ignore character case when parsing, or nil (default) to respect character case.
value
The string to replace matches with.
replace_all
Specify T (default) to replace all matches or nil to replace the first match.
The modified string if successful.
nil on error.
Parse time in h:mm or hh:mm format.
Command: (dos_strregexp "[0-9]?[0-9]:[0-9][0-9]" "1:57")
(0 "1:57")
Parse time in h:mm or hh:mm format, and indicate matching groups.
Command: (dos_strregexp "{[0-9]?[0-9]}:{[0-9][0-9]}" "1:57")
(0 "1:57" (0 "1") (2 "57"))
Parses a seven digit phone number using abbreviations.
Command: (dos_strregexp "\\d\\d\\d-\\d\\d\\d\\d" "My phone number is (206) 555-1212.")
(25 "555-1212")
Parses a seven digit phone number using abbreviations.
Command: (dos_strregexp "\\d+-\\d+" "My phone number is (206) 555-1212.")
(25 "555-1212")
Another matching group example.
Command: (dos_strregexp "product: {.*?};" "product: Bowling ball; price: $199;")
(0 "product: Bowling ball;" (9 "Bowling ball"))