regexMatches
regexMatches(text: String, regex: Regex<T>) : List<{string: String, start: Integer, end: Integer, data: T}>
Gets the list of all matches of regex within text, or the empty list if no matches occur.
Each match is a record with the following fields:
string: StringThe substring that is matched.start: IntegerZero-based character offset intextwhere the match begins.end: IntegerThe first character offset index after the match ends.data: TThe capture groups. This is a record value.
Examples with no capture groups
text | regex | the string of each match |
|---|---|---|
"hello world" | re`\w+` | "hello", "world" |
"abc123def456" | re`\d+` | "123", "456" |
"I have 2 apples and 3 bananas" | re`\d+` | "2", "3" |
"A1 B2 C3" | re`\w\d` | "A1", "B2", "C3" |
"12/05/2024 and 20/08/1991 but not 02/01/1974" | re`\d{2}/\d{2}/\d{4}` | "12/05/2024", "20/08/1991", "02/01/1974" |
"Me: jim@place.com, You: jam@company.org, Them: timothy@hilltop.school" | re`\w+@\w+\.\w+` | "jim@place.com", "jam@company.org", "timothy@hilltop.school" |
"Contact me at 555-123-4567 or 444-987-6543" | re`\d{3}-\d{3}-\d{4}` | "555-123-4567", "444-987-6543" |
"The year is 2024, not 2014 nor 2004 nor 1994 nor 1974" | re`\d{4}` | "2024", "2014", "2004", "1994", "1974" |
Examples with capture groups
text | regex | the data of each match |
|---|---|---|
"hello world" | re`(?<word>\w+)` | {word: "hello"}, {word: "world"} |
"abc123def456" | re`(?<number:Number>\d+)` | {number: 123}, {number: 456} |
"I have 2 apples and 13.582 bananas" | re`(?<count:Float>\d+(?:\.\d+)?)` | {count: 2.0}, {count: 13.582} |
"A1 B2 C3" | re`(?<letter:String>\w)(?<digit:Number>\d)` | {letter: "A", digit: 1}, {letter: "B", digit: 2}, {letter: "C", digit: 3} |
"12/05/2024 and 20/08/1991 but not 02/01/1974" | re`(?<day:Number>\d{2})/(?<month:Number>\d{2})/d{4}` | {day: 12, month: 5}, {day: 20, month: 8}, {day: 2, month: 1} |
"Me: jim@place.com, You: jam@company.org, Them: timothy@hilltop.school" | re`(?<local>\w+)@(?<domain>\w+)\.(?<tld>\w+)` | {local: "jim", domain: "place", tld: "com"}, {local: "jam", domain: "company", tld: "org"}, {local: "timothy", domain: "hilltop", tld: "school"} |
"Contact me at 555-123-4567 or 444-987-6543" | re`(?<area>\d{3})-(?<exchange>\d{3})-(?<station>\d{4})` | {area: 555, exchange: 123, station: 4567}, {area: 444, exchange: 987, station: 6543} |
"The year is 2024, not 2014 nor 2004 nor 1994 nor 1974" | re`(?<year:Number>\d{4})` | {year: 2024}, {year: 2014}, {year: 2004}, {year: 1994}, {year: 1974}, |
See also
Functions
- hasMatch: Checks whether a string matches the structure specified by a regex or not.
- match: Gets a record of information about text that matches the structure specified by a regex,
or otherwise
null. - replaceRegexMatches: Replaces all substring matches of a given regex using the provided replacement template.