substringRegex
Retrieves substring from a given string
using a regex pattern.
substringRegex(searchString, regex)
#
ParameterssearchString
: Thestring
to search withinregex
: The regular expression pattern to search insearchString
#
Returns- The return value depends on the given regular expression. Following rules apply:
- If the regex contains no capture groups, the result is a
list
containing all subsequences matched by the pattern. - If the regex contains unnamed capture groups, substring matched by the groups are added into a unique
list
for every subsequence matched. Thoselist
s are then added into anotherlist
which is returned, i.e alist
oflist
s is returned. - If the regex contains named capture groups, substring matched by the groups are added into a unique
map
for every subsequence matched withmap
's key being the group name andmap
's value being matched substring. Thosemap
s are then added into alist
which is returned, i.e alist
ofmap
s is returned. - If the pattern doesn't match with
searchString
, an emptylist
is returned.
- If the regex contains no capture groups, the result is a
#
Notes about capture groups in pattern:- It's ok to mix both named and unnamed capture groups in the same pattern but when unnamed groups appear with named groups, result contain only named groups and matches of unnamed groups are ignored.
- Named and unnamed groups that don't match, will be skipped from output. Therefore it is recommended to verify existence of
map
keys orlist
elements from the returned value before accessing them.
#
ExamplesNo capture groups
text = 'Hey, you coming for the match?'listOfMatches = substringRegex(text, '[a-z]+')assertTrue(length(listOfMatches) == 6)
Unnamed capture groups
text = `I got this phone on 9th Mar from a mall for $1000, I saved at least 500 bucks.I got this phone on 1st Apr from a mall for $200, I saved at least 30 bucks.`regex = '([1-9][0-9]?(?:st|nd|rd|th)\\s[A-Z][a-z]{2}).*?\\$([1-9][0-9]{2,4}).*?saved[\\s\\w]*([1-9][0-9]{1,3})'result = substringRegex(text, regex)assertTrue(length(result) == 2) # 2 matches for the pattern therefor 2 list items# each item is a list having matchesprint(result[0]) # prints, [9th Mar, 1000, 500]print(result[1]) # prints, [1st Apr, 200, 30]
Named capture groups
text = `I got this phone on 9th Mar from a mall for $1000, I saved at least 500 bucks.I got this phone on 1st Apr from a mall for $200, I saved at least 30 bucks.`regex = '(?<date>[1-9][0-9]?(?:st|nd|rd|th)\\s[A-Z][a-z]{2}).*?\\$(?<price>[1-9][0-9]{2,4}).*?saved[\\s\\w]*(?<saving>[1-9][0-9]{1,3})'result = substringRegex(text, regex)assertTrue(length(result) == 2) # 2 matches for the pattern therefor 2 list items# each item is a map having group names as keyprint(result[0]) # {date: 9th Mar, saving: 500, price: 1000}print(result[1]) # {date: 1st Apr, saving: 30, price: 200}
No match returns an empty list
assertTrue(length(substringRegex("That's a kitten", "[0-9]+")) == 0)
info
Read our notes on regular expression to learn about the regex pattern rules.