Home > other >  Extract substrings in a string using regex if the prefix matches one from the list
Extract substrings in a string using regex if the prefix matches one from the list

Time:01-19

The prefixes are Dog, Cat, Bird, horse and they end with numbers.

The output should only be Dog1234, Cat4567, Bird2342344, Horse898087, Dog2345, Bird2340988

$text = 'I Dog1234 need Cat4567 do Bird2342344 extract Horse898087 the  Dog2345 strings that contain the a set of prefixes using regex Bird2340988'

$mymatchedStrings = $text | Select-String -Pattern '.*([Dog|Cat|Bird|Horse]\d ).*' -AllMatches

$mymatchedStrings.Matches[1]

CodePudding user response:

You can use

Select-String '\b(?:Dog|Cat|Bird|Horse)\d \b' -input $text -AllMatches | Foreach {$_.matches.value}

Details:

  • \b - word boundary
  • (?:Dog|Cat|Bird|Horse) - a non-capturing group matching one of the listed alternative words
  • \d - one or more digits
  • \b - word boundary.

Notes:

  • .*(...).* patterns match the entire line (or string depending on the context and flags) and thus should not be used in cases when you expect to match multiple substrings in a longer text (as it is the case here)
  • [Dog|Cat|Bird|Horse] is a character class, you need a grouping construct to match alternative multicharacter substrings
  • -AllMatches returns all matched occurrences, not just the first one
  • Foreach {$_.matches.value} returns the match text only, not the whole match data info.
  • Related