Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on portion of matched regular expression (python)

Tags:

python

regex

Say I have a string 'ad>ad>ad>>ad' and I want to split on this on the '>' (not the '>>' chars). Just picked up regex and was wondering if there is a way (special character) to split on a specific part of the matched expression, rather than splitting on the whole matched expression, for example the regex could be:

re.split('[^>]>[^>]', 'ad>ad>ad>>ad')

Can you get it to split on the char in parenthesis [^>](>)[^>] ?

like image 864
dpdenton Avatar asked Jul 27 '26 10:07

dpdenton


2 Answers

You need to use lookarounds:

re.split(r'(?<!>)>(?!>)', 'ad>ad>ad>>ad')

See the regex demo

The (?<!>)>(?!>) pattern only matches a > that is not preceded with a < (due to the negative lookbehind (?<!>)) and that is not followed with a < (due to the negative lookahead (?!>)).

Since lookarounds do not consume the characters (unlike negated (and positive) character classes, like [^>]), we only match and split on a < symbol without "touching" the symbols around it.

like image 90
Wiktor Stribiżew Avatar answered Jul 29 '26 22:07

Wiktor Stribiżew


Try with \b>\b

This will check for single > surrounded by non-whitespace characters. As the string in the question is continuous stream of characters checking word boundary with \b is simplest method.

Regex101 Demo


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!