Relative Content

Tag Archive for pythonregex

Regex to match a whole number not ending in some digits

I’ve not been able to construct a pattern which can return an whole numbers that don’t end in a sequence of digits. The numbers could be of any length, even single digits, but they will always be whole. Additionally, multiple numbers could be on the same line of text and I want to match them all. The numbers are always followed by either a single space or the end of the line or the end of the text. I’m matching in python 3.12

Escaping regex string

I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex?

How to use python regex to get the first occurrence of a substring [duplicate]

This question already has answers here: Regular expression to stop at first match (10 answers) Splitting on first occurrence (5 answers) Closed last month. I have the following script trying to get the first occurrence of the word “symbol”, but it keeps returning the last one (print “symbol in the middle and end”). How can […]

in python using re.sub how do i interpolate text [duplicate]

This question already has answers here: Adding characters after a certain word in Python using regex (3 answers) Closed last month. I want to scan for the situation where a certain string ‘INC0459444’ is next to a date as in the following example INC04594442024-06-05 If this is the case, I want to interpolate the text […]

Find expression if other expression is in string

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 […]