expression1 = r'd+ed+e' #e.g '10e2e' or '30e4e'
expression2 = r'd+%/d+%' #e.g '30%/1%' or '1%/2%
For a given string, I want to extract expression1
only if expression2
is present in the string.
Example:
string1 = '30e4e 1%/2%' -> re.search(correctRegex, string1, re.X).group(0) #should return '30e4e'
string2 = '30e4e blabla' -> re.search(correctRegex, string2, re.X) #should be None
string3 = '1%/2% 30e4e ' -> re.search(correctRegex, string1, re.X).group(0) #should return '30e4e'
I’ve tried re.search(r'^(?=.*d%/d+%).*?(d+ed+e)', '1%/2% 10e2e').group(0)
but it returns 1%/2% 10e2e
, I want .group(0)
to return only the expression I am looking for.
12
Regex
(?=.*d+%/d+%).*?(d+ed+e)
The lookahead (?=.*d+%/d+%)
allows to check for the expression2.
.*?
is for matching the expression1 even if there is shifted from the start of the expression2. And it’s non-greedy ?
allow to get more then only 1 digit for the first number d+
.
Code for testing
import re
# Strings
string1 = '30e4e 1%/2%'
string2 = '30e4e blabla'
string3 = '1%/2% 30e4e'
# Define the regex pattern
pattern = r'(?=.*d+%/d+%).*?(d+ed+e)'
# Apply the regex to each string
match1 = re.search(pattern, string1)
match2 = re.search(pattern, string2)
match3 = re.search(pattern, string3)
# Display the capture group only if there is a match (to avoid execution error)
if match1:
print(f"Match 1: {match1.group(1)}")
if match2:
print(f"Match 2: {match2.group(1)}")
if match3:
print(f"Match 3: {match3.group(1)}")
Result
Match 1: 30e4e
Match 3: 30e4e