python-re

Regex string parsing: pattern starts with ; but can end with [;,)%&@]

Regex string parsing: pattern starts with ; but can end with [;,)%&@] Question: I am attempting to parse strings using Regex. The strings look like: Stack;O&verflow;i%s;the;best! I want to parse it to: Stack&verflow%sbest! So when we see a ; remove everything up until we see one of the following characters: [;,)%&@] (or replace with empty …

Total answers: 1

How to match a regex expression only if a word is present before or after

How to match a regex expression only if a word is present before or after Question: I’m really struggling with some regex. I’ve had a good look at similar questions and I can’t work out why it’s not working! I’m trying to match the string ‘ok’ when it is preceded by 4 digits ((?<=d{4}s)ok) but …

Total answers: 2

Python regex doesn't match when adding additional text around pattern and text

Python regex doesn't match when adding additional text around pattern and text Question: So I’m trying to match "Python 3.11.4 (64-bit) Setup" like so: re.match(r"Python (d.)+d (64-bit) Setup", "Python 3.11.4 (64-bit) Setup") However, for some reason, it doesn’t work. But, when I try re.match(r"(d.)+d", "3.11.4") it matches perfectly well. How do I fix this? P.S.: …

Total answers: 2

python regex not capturing variables, but regex working

python regex not capturing variables, but regex working Question: I am trying to create a data frame with variables: bidder_rank, bidder_id, bid_total, bidder_info. I have created a regex pattern, which seems to work on regex101. However, the Python script has been breaking for a reason I cannot figure out. # imports import os import pandas …

Total answers: 2

Matching and replace multiple strings in python

Matching and replace multiple strings in python Question: import re text = ‘{"mob":"1154098431","type":"user","allowOrder":false,"prev":1}’ newstring = ‘"allowOrder":true,"’ #newstring = ‘"mob":"nonblocked",’ reg = ‘"allowOrder":(.*?),"|"mob":"(.*?)",’ r = re.compile(reg,re.DOTALL) result = r.sub(newstring, text) print (result) im trying to matching and replacing multiple regex patterns with exact values can someone help me to achieve this Asked By: Annie Chain || …

Total answers: 1

Search string that contains WORD with W or ^ before and W or $ after

Search string that contains WORD with W or ^ before and W or $ after Question: How to simplify following regular expression re.search(f’W{word}W’, text) or re.search(f’^{word}W’, text) or re.search(f’W{word}$’, text) or word == text i.e. return True for any string that contains word with W or ^ before and W or $ after. Variants re.search(f'[^W]{word}[W$]’, …

Total answers: 1

Python: regex pattern fails after being split into multiple lines

Python: regex pattern fails after being split into multiple lines Question: I have a regex pattern that works fine if I write it in a single line. For instance, the pattern work if I do the following: MAIN_TREE_START_STRING_PATTERN = ( r"*{3}s+bTotal Thingsb:s+(?P<number_of_things>d+)" ) compiled_pattern = re.compile(MAIN_TREE_START_STRING_PATTERN, flags=re.X) match = compiled_pattern.match(string="*** Total Things: 348, abcdefghi ***") …

Total answers: 2

PYTHON – Find a number preceded by specific string

PYTHON – Find a number preceded by specific string Question: I have a text, where I want to find numbers (could be any number), preceded by specific word. (for example "your debt is 100 dollars) I want to find a way to find "is 100". I have already found the way to find a number …

Total answers: 3

Remove double quotes only if it between character Python

Remove double quotes only if it between character Python Question: I have a string inside variable a contains double quotation inside double quotations : Input : a = ""comment_text": "Siti Sa"diah I need this for my neighbor needs"" Expected result : a = ""comment_text": "Siti Sadiah I need this for my neighbor needs"" I tried …

Total answers: 2