Python regex with variable {}-multiplier [duplicate] - python

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 6 years ago.
Say you wanted to create a pattern that matches sequences of var consecutive digits. You could do it this way:
p = re.compile(r"\d{"+str(var)+"}")
or this way:
p = re.compile(r"\d{%d}" % var)
But how would you do it using format()?
I tried both:
p = re.compile(r"\d{0}".format(var))
and:
p = re.compile(r"\d{{0}}".format(var))
but none of these worked.

You need to actually have triple { and } - two for the escaped literal braces and one for the placeholder:
In [1]: var = 6
In [2]: r"\d{{{0}}}".format(var)
Out[2]: '\\d{6}'

Related

python multiple characters replace in string not working with pipe [duplicate]

This question already has answers here:
How to input a regex in string.replace?
(7 answers)
Closed 1 year ago.
I am trying to match and replace multiple characters in a string.
str1 = 'US$0.18'
reg = 'AUS$|US$|HK$|MK$'
#reg = 'AUS\$|US\$|HK\$|MK\$' <-- doesn't work
#reg = 'US$' <-- this works
str1 = str1.replace(reg, '')
This doesn't replace US$ which I expected to.
What am I missing here?
You can do that using re.sub(). Since $ has a special meaning in re, we need to escape it by appending a \ in front of it.
(AUS|US|HK|MK)\$ - Finds a match that has either of AUS, US, HK or MK that is followed by a $.
re.sub(r'(AUS|US|HK|MK)\$',r'', s) - Replaces the matched string with a '' of string s.
import re
s = "US$0.18 AUS$45 HK$96"
x = re.sub(r'(AUS|US|HK|MK)\$',r'', s)
print(x)
0.18 45 96

How to find 6 digits in a string with a particular pattern on the first 3 digits in Python? [duplicate]

This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 2 years ago.
I am trying to find a regular expression to return to me the entire 6 digits with the first 3 digits as a pattern/fixed.
Ex:
import re
string_ex = 'docs/data/622999/2013904065003_file.bin'
re.findall(r'622(\d{3})',string_ex)
results in just ['999']
but I want the result to be ['622999']
Thanks!
You should include 622 too within the parenthesis
>>> import re
>>> string_ex = 'docs/data/622999/2013904065003_file.bin'
>>> re.findall(r'(622\d{3})',string_ex)
['622999']
You can use "index" on the string directly.
i = string_ex.index("622")
found = string_ex[i-3:i+2]
https://www.tutorialspoint.com/python/string_index.htm

Python regex and escape characters [duplicate]

This question already has answers here:
Regular expression with backslash in Python3
(2 answers)
Closed 3 years ago.
I have the following regex code:
tmp = 'c:\\\\temp'
m = re.search(tmp, tmp)
if(m==None):
print('Unable to find a ticker in ' + filename)
else:
print("REGEX RESULT - " + m.group(0))
which returns None. No matter how many or few backslashes I use for variable tmp, I still get None as result. How can I perform regex to search for a backslashed file path?
you can use r'' to ignore escape chars
tmp = r'c:\\temp'
r is for raw string

Python - Concat two raw strings with an user name [duplicate]

This question already has answers here:
Why can't Python's raw string literals end with a single backslash?
(13 answers)
Why can't I end a raw string with a backslash? [duplicate]
(4 answers)
Closed 3 years ago.
I have a raw string like this,
MasterFile_Name = r'C:\Users\ABC\X12345\DEF\File - Test.xlsx'
I want to pass the value of X12345 through a variable.To do that I am doing something like this
MyID = X12345
MasterFile_Name = r'C:\Users\ABC\' + MyID + '\DEF\File - Test.xlsx'
and
MasterFile_Name = r'C:\Users\ABC\' + MyID + r'\DEF\File - Test.xlsx'
They both are not working for me.
Kindly help me with this.
If the intention is to just concatenate it.
using str.format():
MyID = 'X12345'
MasterFile_Name = r'C:\Users\ABC\{}\DEF\File - Test.xlsx'.format(MyID)
print(MasterFile_Name)

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Categories

Resources