Skip to content

Tag: regex

Capture substring and send it to a function that modifies it and can replace it in this string

Incorrect output that I am getting, because if I incorrectly capture the substrings, the replacements will also be incorrect Having well-defined limits, I don’t understand why this capture pattern try to capture beyond them? And the output that I need is that: Answer There are several errors in your code, among which: You are printing the result of the one_day_or_another_day_relative_to_a_date_func

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

how to define selection condition in regex in python

I am having a string in which some binary numbers are mentioned. I want to count number of occurrence of given pattern, but I want set my pattern above 7 digits of character, so the result should show only more than 7 characters. it means how I can set my pattern selection, so it should count only 7 digits and

split on delimeter and ignore a pattern

I would like to split a string based on a delimiter and ignore a particular pattern. I have lines in a text file that look like so I would like to split on “|” but ignore 0 and 567 and grab the rest. i.e whenever I split, its grabbing the two numbers as well. now numbers can occur in other

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

how to have re.sub work for multiple pattern replace on list of list?

I have a list of list input- old_list: my desired output – new_list: and I have tried 1. and replace(new_list, old, new) for … but none of them works, the output is the same as the original old_list. Any suggestions? Thanks! Answer You need to use output of each iteration as input for a next iteration, i.e. in new_list instead

Regex removes certain words from my string – Python

The below code is to lookup a dictionary and replace string with values corresponding to dict’s key. Can someone help me understand why my code omits certain words? It removes lh preceeded and followed with a . i.e., lh. and .lh. How to overcome this? I get the output left hand l.h. -left hand- l.h plh phli lhp 1lh lh1