Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If-Then-Else regex statement

I am trying to form a regular expression that would capture <expression1> if it is in the string otherwise capture <expression2>.

I tried something along the lines of: (IF)(?(1)THEN|ELSE), meaning the capture would be IFTHEN (in case IF is found) or ELSE (in case IF is not found)

For example:

(apple1\d)(?(1)|apple2\d)

case1: for the string: pear33 apple14 apple24 orange22 orange44

Result would be: apple14

case2: In contrast for the string: pear33 apple24 orange22 orange44

The result would be: apple24 (since there is no apple1 it would capture apple2\d)

My regex works well for case1 it returns apple14 however the ELSE doesn't work. I expect it to return apple24 for case2

like image 364
jadeidev Avatar asked Aug 08 '26 00:08

jadeidev


1 Answers

To start off, I'm not sure why you'd need an if-else statement for this (See version 2 of my answer), but I'll try to provide a few solutions.

So, for me, @Barmer's solution (If-Then-Else regex statement) gave me error: bad character in group name although I'm sure with proper tweaking that may be the optimal solution.

Until he gets back, however, you can try these (although search.group() and search.groups() do annoy me a bit regarding their handling of capture groups/lack thereof)

.

VERSION 1: Ultra specific version, based on the solutions suggested above. My solution here is not desirable in my opinion.

>>> import re


>>> string1 = 'pear33 apple14 apple24 orange22 orange44'
>>> string2 = 'pear33 apple24 apple14 orange22 orange44'


>>> re.findall('(?<!apple[12]\d)[\s]+(apple1\d|apple2\d)', string1)
['apple14']
>>> re.findall('(?<!apple[12]\d)[\s]+(apple1\d|apple2\d)', string2)
['apple24']


>>> re.search('(?<!apple[12]\d)[\s]+(apple1\d|apple2\d)', string1).group()
' apple14'
>>> re.search('(?<!apple[12]\d)[\s]+(apple1\d|apple2\d)', string2).group()
' apple24'

VERSIONS 2 AND 3: Way better and more scalable versions in my opinion. I'm privy to version 2. TBH, though, this solution can lead to memory tie ups, but for short strings it will work fine

>>> string1 = 'pear33 apple14 apple24 orange22 orange44'
>>> string2 = 'pear33 apple24 apple14 orange22 orange44'


>>> re.findall('[\S\s]*?(apple[\d]+)[\S\s]*', string1)
['apple14']
>>> re.findall('[\S\s]*?(apple[\d]+)[\S\s]*', string2)
['apple24']


>>> re.findall('(?<!apple\d\d)[\S\s]+?(apple[\d]+)[\S\s]*', string1)
['apple14']
>>> re.findall('(?<!apple\d\d)[\S\s]+?(apple[\d]+)[\S\s]*', string2)
['apple24']
like image 116
FailSafe Avatar answered Aug 09 '26 13:08

FailSafe



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!