Python regular expression for matching word before <

So I want to map something like this -

foo <TEST>something something </TEST> blah

I want the regex to get me foo, but not get me. I was thinking about using regex, something like this

(\w\s)<

with a negative look, but I'm not sure how to use this in this case.

OTHER CASE -

something something foo <TEST> something something </TEST> blah 
+3
source share
3 answers

You can try something like this:

\w+(?=\s*<[^/])

demo version of regex101

A positive lookahead (?=\s*<[^/])ensures that there are additional spaces that follow <, which are not followed /in front.

\w+     matches one or more \w
(?=     beginning of positive lookahead
  \s*   optional spaces
  <     a < character
  [^/]  not a / character
)       end of positive lookahead
+5
source

A ghostly look will be very good.

(\S+)\s*<(?!/)
+1

^(?P<Word>\S*\W)*<

http://regex101.com/r/tS6hM2

0

All Articles