Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex to match word before <

Tags:

python

regex

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 
like image 929
praks5432 Avatar asked Sep 03 '25 10:09

praks5432


2 Answers

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
like image 57
Jerry Avatar answered Sep 05 '25 01:09

Jerry


Negetive look-ahead will do just fine.

(\S+)\s*<(?!/)
like image 38
Shiplu Mokaddim Avatar answered Sep 05 '25 02:09

Shiplu Mokaddim