Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading