Python regex to match word before <

python, regex

Solution

You could try something like this:

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

regex101 demo

The positive lookahead `(?=\s*<[^/])` ensures that there are optional spaces followed by a `<` which is not followed by a `/` ahead.

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

Problem

So I want to match something like this - ``` foo <TEST>something something </TEST> blah ``` I want a regex that gets me the foo, but does not get me the something. I was thinking of using a regex that was something like this ``` (\w\s)< ``` with a negative lookahead, but I'm not sure how to use that in this case. OTHER CASES- ``` something something foo <TEST> something something </TEST> blah ```

Original source