Skip to content
Advertisement

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 this with an if/else but was wondering how to use a

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 either occur singularly as xxxx, or they occur as

python re.split function, how do I return the full character set?

I’m trying to use a regex pattern split this string into chunks seperated by any character. This prints [”, ’12’, ’56’, ‘1’] How do I use the split function to have it output the whole string, delimited by any character? IE [‘a12’, ‘b56’, ‘c1’] Answer Try to use re.findall instead re.split (regex101): Prints:

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 indulgent im a

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 returned as part of the resulting

Re Regular expression operations, remove periods?

I’m working with a function I made to split this sample line below to remove the standalone numerical values (123), however it’s also removing the trailing numbers which I need. I also can’t figure out how to remove the “0.0” ABC/0.0/123/TT1/1TT// What’s coming out now is below, notice the 0. and “TT” that’s missing the trailing 1. [ABC], [0.], [TT],

Why isn’t my re.sub finding all instances using my regex?

I’m using Python 3.10 on Windows 10 and trying the search below: If I use just “JohnnyB Cool”, the “B” gets a space before it. Why isn’t the “JohnnyB” substituted in the first search? I’ve also tried: To be clear, I want the final answer to be, Johnny B Cool & Joe Cool. Answer You may use this python code:

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, fish] [Alonso, Alicia, Bob, Lynn] , [red,

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 …. This is called a negative lookbehind

Advertisement