Garbage characters added after re.sub()? [duplicate] - python

This question already has an answer here:
How to apply a function on a backreference? [duplicate]
(1 answer)
Closed 5 years ago.
Why the following regular expression returns garbage !!
expr = 'a + b'
expr2 = re.sub(r'\w', 'probs["\1"]', expr)
probs[""] + probs[""]
or:
probs["\x01"] + probs["\x01"]
desired output :
probs["a"] + probs["b"]
Stupid me I forgot the brackets :
expr2 = re.sub(r'(\w)', r'probs["\1"]', expr)

\1 is being interpreted as the character with ascii value 1. You probably want to add an r to make it a raw string, or use \\1 in the string.

Related

Split string on "$" using regex [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 1 year ago.
I am trying to split a string using regex on $ symbol but the output is not what I want.
string = "43$hello"
list_of_splits = re.split("$",string)
Output:
['43$hello','']
Output I want:
['43','hello']
It's visible by the output that "$" is a special character in regex, but now by how can I do this?
Use the escape character \ : list_of_splits = re.split("\$", str)
You can just use string split method.
string = "43$hello"
string.split("$")
Output
['43', 'hello']

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

Replacing a char at provided index in a string [duplicate]

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Changing one character in a string
(15 answers)
Closed 4 years ago.
So I have this string: "My Wurds" and I'd like to replace the u at index 4 with o. what is the proper Python way to do that?
You could build a new string using slices:
s = "My Wurds"
t = s[:4] + "o" + s[5:]
If you know the index of u where you need to replace with o, a pretty straight approach would be:
s[:4] + s[4:].replace('u', 'o', 1)
replace('u', 'o', 1) will ensure replace of only first occurance.

How can I add a space after a specific character in a string? [duplicate]

This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 6 years ago.
I need to add a space between X in a string. The program takes measurements in one field and I need to be able to separate integers from the "x" before doing the calculation.
For example: "12x24" should read "12 x 24"
Replace 'x' with '<space>x<space>' using str.replace() function as:
>>> my_str = '12x24'
>>> my_str.replace('x', ' x ')
'12 x 24'
Use the replace method to substitute ' x ' for 'x':
string.replace('x', ' x ')

How to get the correct result when I use re.compile(pattern), if the pattern contains some special characters, like, (),? [duplicate]

This question already has answers here:
Escaping regex string
(4 answers)
Closed 7 years ago.
I am trying to replace a string in a file using python re lib. But I failed on replacing some texts with some special characters, like, (), ?, etc. Can anyone help me look at this issue?
I attached my code in here.:
filterText = '\"' + sheet.row_values(row)[1] + '\"';
print "filterText = %s"%filterText;
pattern = re.compile(filterText, re.S);
replacedText = '\"' + sheet.row_values(row)[2] + '\"';
print "replacedText = %s"%replacedText;
if filterText == "English (UK)":
print "replacedText = %s"%replacedText;
fileContent = re.sub(pattern, replacedText, fileContent);
re.escape(string)
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
Use re.escape to convert any string as a literal pattern.
filterText = '\"' + re.escape(sheet.row_values(row)[1]) + '\"'

Categories

Resources