Home > Blockchain >  Regex extract word starting with a set string and ending the line or ending with ;
Regex extract word starting with a set string and ending the line or ending with ;

Time:10-04

I'd like to extract one or more word from a string containing multiple words matching the same pattern (regex python). Here's the line:

new_appointment_requested; general; SAT-newlead

From that line, I need to extract "SAT-" and whatever comes after it within that word. In this case the output should be "SAT-newlead". It can also be SAT-oldlead or something completely different, and there can be more than one word containing this pattern at a time. To capture all possible scenarios:

  1. find the word starting with "SAT-" (\b)
  2. if it is the final or only word in the string, extract the rest of the word,
  3. if there is a ; after the word (it's not the final word in the string), grab the word without the ;
  4. if more than one word matches this pattern, extract all instances as separate words

For some reason I can't wrap my head around Regex, so any help on this would be appreciated.

CodePudding user response:

Match SAT and everything not a space, semicolon or newline:

\bSAT[^ ;\n]*

See live demo.

  • Related