Questions tagged [python-re]
Python library that provides regular expression matching operations similar to those found in Perl.
python-re
2,069
questions
3
votes
0
answers
35
views
Why does re.compile exist?
Here is re.compile:
>>> import re, inspect
>>> print(inspect.getsource(re.compile))
def compile(pattern, flags=0):
"Compile a regular expression pattern, returning a Pattern ...
1
vote
1
answer
19
views
Is there any situation where re.search could not be used instead of re.match? [duplicate]
The documentation seems clear but it begs the question, what is the purpose of re.match? Couldn't re.search with the caret (^) be used instead as long as the MULTILINE flag is not enabled? Is re.match ...
1
vote
2
answers
49
views
How to find all occurrences of a substring in a string while ignoring some characters in Python?
I'd like to find all occurrences of a substring while ignoring some characters. How can I do it in Python?
Example:
long_string = 'this is a t`es"t. Does the test work?'
small_string = "test&...
1
vote
1
answer
46
views
How to extract the volume from a string using a regular expression?
I need to extract the volume with regular expression from strings like
"Candy BAR 350G" (volume = 350G),
"Gin Barrister 0.9ml" (volume = 0.9ml),
"BAXTER DRY Gin 40% 0.5 ml&...
2
votes
1
answer
46
views
How can I simplify this method to replace punctuation while keeping special words intact?
I am making a modulatory function that will take keywords with special characters (@&\*%) and keep them intact while all other punctuation is deleted from a sentence. I have devised a solution, ...
2
votes
1
answer
52
views
How do I fix this Reg ex so that it matches hyphenated words where the final segment ends in a consonant other than the letter m
I want to match all cases where a hyphenated string (which could be made up of one or multiple hyphenated segments) ends in a consonant that is not the letter m.
In other words, it needs to match ...
-3
votes
1
answer
17
views
Replacing part of string with re.sub with number and string [closed]
I want to replace part of a string based on re.sub method as below
import re
re.sub("([0-9]_F)$", '[0-9]_DO', 'sdsd3_F')
However I fail to manage the numerical part of match which is also a ...
0
votes
1
answer
49
views
How can a number range and value be extracted from this complicated string using Python?
I have a complicated string that includes a kilometer range and a fee for users that fall into that range. Ideally, I would like to transform the string into something that I could use to easily ...
0
votes
0
answers
31
views
Match a pattern containing multiple subpatern in different order [duplicate]
In Python with Re library, I try to match a char structur with this synopsis:
[n|new|newgame] <name> [code=<code>] [type=<type>] [licence=<licence>] [url=<url>]
But code,...
-1
votes
0
answers
28
views
Can't get the expected output of "dir" command using wexpect on python
Can't get the expected output of "dir" command using wexpect on python.
My wexpect script:
import wexpect
def run_cmd():
# Start cmd.exe process
cmd = wexpect.spawn("cmd.exe&...
-2
votes
1
answer
30
views
regular expression to find pattern in the same word [duplicate]
There is a string "123:987 767687:99 145:986 156:876 "
My regex expression is (\d{3}):\1
I expecting the result is 123:987, 145:986, 156:876
there is no result found. i dont undertsand. ...
0
votes
1
answer
38
views
Match a patern with multiple entries in arbitrary order in Python with re [duplicate]
I try to catch values entered in syntax like this one name="Game Title" authors="John Doe" studios="Studio A,Studio B" licence=ABC123 url=https://example.com command=&...
-3
votes
0
answers
22
views
Why is \b NOT matching beginning of word after a space? [duplicate]
According to the docs for \b:
"r'\bat\b' matches 'at', 'at.', '(at)', and 'as at ay'"
... except this is not my experience:
import re
pat = re.compile(r'\bat\b')
print(pat.match('as at ay'))
...
4
votes
1
answer
87
views
Why is `re.Pattern` generic?
import re
x = re.compile(r"hello")
In the above code, x is determined to have type re.Pattern[str]. But why is re.Pattern generic, and then specialized to string? What does a re.Pattern[...
0
votes
1
answer
54
views
How does regex filteration work in Python re while logging sensitive info?
I am trying to write a python script which would redact/hide certain data present in a string before logging it out to the console. Below is my code snippet so far.
import re
from logging import DEBUG,...