Skip to main content

substringRegex

Retrieves substring from a given string using a regex pattern.

substringRegex(searchString, regex)

Parameters#

  • searchString: The string to search within
  • regex: The regular expression pattern to search in searchString

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. Those lists are then added into another list which is returned, i.e a list of lists is returned.
    • If the regex contains named capture groups, substring matched by the groups are added into a unique map for every subsequence matched with map's key being the group name and map's value being matched substring. Those maps are then added into a list which is returned, i.e a list of maps is returned.
    • If the pattern doesn't match with searchString, an empty list is returned.

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 or list elements from the returned value before accessing them.

Examples#

  • No 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.