regex-lookarounds

Need help to understand the star quantifier (*) output

Need help to understand the star quantifier (*) output Question: I am able to understand output of the below command: import re text = "streets2345" pattern = r"d+" match = re.search(pattern, text) print(match.group(0)) Output: 2345 However, I am not able to understand why the below code is returning null. import re text = "streets2345" pattern …

Total answers: 1

Regex to append a word after the occurance of any word from a list

Regex to append a word after the occurrence of any word from a list Question: Recently I got this amazing answer on how to remove a word if it occurs after any word in word list1. I wanted to remove the word eat. I was wondering if it is possible to do the reverse, add …

Total answers: 1

Remove a pattern if does not contains a specific words

Remove a pattern if does not contains a specific words Question: I need to remove everything from the given text after a specific pattern if doesn’t include specific words. For example, I need to remove everything after a number if doesn’t include "key1" and "key2" txt1 = "this is a number 123456789 and there aren’t …

Total answers: 1

Python regex: Explain why expression not matching

Python regex: Explain why expression not matching Question: I am using regex library to find words that are in between specific other words, for example, I want to match "world" if and only if a greeting precedes it and punctuation follows. To avoid matching word prefixes and suffixes, I added the additional condition [^a-zA-Z]. However, …

Total answers: 2

Python regex: Negative look-ahead with selection of different length strings

Python regex: Negative look-ahead with selection of different length strings Question: I’m searching for a Regex pattern in Python 3.8 that matches a sequence of strings if and only if it’s not followed by a selection of other strings. For example, I want to match the pattern "<fruit> and <fruit>" only if the second fruit …

Total answers: 1

RegEx for extracting domains and subdomains

RegEx for extracting domains and subdomains Question: I’m trying to strip a bunch of websites down to their domain names i.e: https://www.facebook.org/hello becomes facebook.org. I’m using the regex pattern finder: (https?://)?([wW]{3}.)?([w]*.w*)([/w]*) This catches most cases but occasionally there will be websites such as: http://www.xxxx.wordpress.com/hello which I want to strip to xxxx.wordpress.com. How can I identify …

Total answers: 4

Regular Expression Matching First Non-Repeated Character

Regular Expression Matching First Non-Repeated Character Question: TL;DR re.search(“(.)(?!.*1)”, text).group() doesn’t match the first non-repeating character contained in text (it always returns a character at or before the first non-repeated character, or before the end of the string if there are no non-repeated characters. My understanding is that re.search() should return None if there were …

Total answers: 4

Python Regex Engine – "look-behind requires fixed-width pattern" Error

Python Regex Engine – "look-behind requires fixed-width pattern" Error Question: I am trying to handle un-matched double quotes within a string in the CSV format. To be precise, "It "does "not "make "sense", Well, "Does "it" should be corrected as "It" "does" "not" "make" "sense", Well, "Does" "it" So basically what I am trying to …

Total answers: 3