Home > database >  REGEX: how to i get the name more the character " : "
REGEX: how to i get the name more the character " : "

Time:11-03

Im using python to extract some info

i wanna get the words/names before the charcter : but the problem is everythig is tied together

from here

Morgan Stanley.Erik Woodring: 

i just wanna extract "Erik Woodring:"

or from here

market.Operator: 

i just wanna extract Operator:

sometimes there are questiosn like this

to acquire?Tim Cook:

i just wanna extract "Tim Cook:"

this is what i tried

\w*(?=.*:)

this is not getting what i wanted, its returning a lot of words

CodePudding user response:

This could be the regex you're looking for:

\b[\w\s] (?=:)
  • \b world boundary;
  • [\w\s] matches any word or whitespace (at least one character);
  • (?=:) positive lookahead that specifies the word must be followed by a punctation mark;

https://regex101.com/r/w86oWv/1

If you want to get the ":" too you can simply remove the lookahead:

\b[\w\s] :
  • Related