Skip to content

Tag: regex

Match everything except certain strings

I am trying to write python script to match a regex that can include everything which has two – and one . but I also want to exclude two strings from it. They are NIST-Privacy-v1.1 and NIST-CSF-v1.1 Here is my sample data: I started with a very simple regex which does the job of matching what I need but…

Regex for Alternating Numbers

I am trying to write a regex pattern for phone numbers consisting of 9 fixed digits. I want to identify numbers that have two numbers alternating for four times such as 5XYXYXYXY I used the below sample I tried the below pattern but it is not accurate can someone point out what i am doing wrong? Answer I woul…

Execute f-string in function

I have a and want to replace all tokens starting with with a new token I wrote a function: If I execute the lines separately it works. But the function itself doesn’t work. I’d expect How can I “exec” f-string in a function? Answer You are overcomplicating this. Simply reassign x: But …

regex whole string match between numbers

I want to extract a whole word from a sentence. Thanks to this answer, I can get whole words in cases like: where symbols next to the word don’t bother. However if there’s a number it doesn’t find the word. How should I modify the expression to match cases where there’s a number next t…

How to remove white space in between ascii and nonascii chars?

For example: I want to find at least 3 ascii chars, followed by a space, then followed by a nonascii char, and replace the white space with empty string. My code has two issues: How to write the replacement string for (s)? How to make it also work for the reverse order of s2?: [^a-zA-Z0-9] Answer Put the stri…