Home > OS >  regex: a pattern that's start with a word and finish with ":"
regex: a pattern that's start with a word and finish with ":"

Time:04-16

I would like to have the correct regex code for patterns that are like this one, it starts with words until this symbol ":" example : "This is: a pattern" the regex code that I want has to match from only the starting words to the symbol ":" ( for the example that I provided, I want to match "This is")

CodePudding user response:

Something like this should work

r'\b[^:]*(?=:)'

Details:

  • \b asserts that the match must occur on a boundary of a word (i.e asserts position at the edge of a word).

  • [^:]* matches zero or more repetitions of characters other than the :.

  • (?=:) is a positive lookahead assertion (checks if we have a : but does not actually consume it).

  • Related