Skip to content

Tag: python-re

Extract all matches unless string contains

I am using the re package’s re.findall to extract terms from strings. How can I make a regex to say capture these matches unless you see this substring (in this case the substring “fake”). I attempted this via a anchored look-ahead solution. Current Output: Desired Output I could accomplish …

Reinstate lost leading zeroes in Python list

I have a list of geographical postcodes that take the format xxxx (a string of numbers). However, in the process of gathering and treating the data, the leading zero has been lost in cases where the postcode begins with ‘0’. I need to reinstate the leading ‘0’ in such cases. Postcodes …

Function with two loops

I want to make a function what must have 2 loops: must check that at least 2 characters (letters) are inserted if the two characters are in the form that the variable must receive at the end, the variable receives the correct orthographic form. Here is my code (but it throws me in infinite loop) [Please be in…

combining split with findall

I’m splitting a string with some separator, but want the separator matches as well: I can’t find an easy way to combine the 2 lists I get: Into the desired output: Answer From the re.split docs: If capturing parentheses are used in pattern, then the text of all groups in the pattern are also retur…

Output of split is not what I was expecting

I’m just learning python, and I’m having some problems reading a .txt file that I created. My objective: I have a txt file with a list of strings. I’m trying to read, process it and save every letter into a new list. example2.txt file: [one, two, THREE, one, two, ten, eight,cat, dog, bird, f…

Unmatch escaped braces

Which regular expression can I use to find all }, all while excluding escaped }? I.e. how do I get as only match: Answer The following pattern should achieve your goal: The pattern you’re looking for is (?<!…): Matches if the current position in the string is not preceded by a match for ……