Reputation: 887
I want to make the function which find for string in the array and then replace the corres[ponding element from the dictionary. so far i have tried this but i am not able to figure out few things like
\1
but it didn't workdsds
def myfunc(h):
myarray = {
"#":"\\#",
"$":"\\$",
"%":"\\%",
"&":"\\&",
"~":"\\~{}",
"_":"\\_",
"^":"\\^{}",
"\\":"\\textbackslash{}",
"{":"\\{",
"}":"\\}"
}
pattern = "[#\$\%\&\~\_\^\\\\\{\}]"
pattern_obj = re.compile(pattern, re.MULTILINE)
new = re.sub(pattern_obj,myarray[\1],h)
return new
Upvotes: 0
Views: 108
Reputation: 2342
r""
) for better readability of the code.str.replace
function instead of re.sub
.def myfunc(h):
myarray = [
("\\", r"\textbackslash"),
("{", r"\{"),
("}", r"\}"),
("#", r"\#"),
("$", r"\$"),
("%", r"\%"),
("&", r"\&"),
("~", r"\~{}"),
("_", r"\_"),
("^", r"\^{}")]
for (val, replacement) in myarray:
h = h.replace(val, replacement)
h = h.replace(r"\textbackslash", r"\textbackslash{}", h)
return h
The code is a modification of @tigger's answer.
Upvotes: 1
Reputation: 215039
You're looking for re.sub callbacks:
def myfunc(h):
rules = {
"#":r"\#",
"$":r"\$",
"%":r"\%",
"&":r"\&",
"~":r"\~{}",
"_":r"\_",
"^":r"\^{}",
"\\":r"\textbackslash{}",
"{":r"\{",
"}":r"\}"
}
pattern = '[%s]' % re.escape(''.join(rules.keys()))
new = re.sub(pattern, lambda m: rules[m.group()], h)
return new
This way you avoid 1) loops, 2) replacing already processed content.
Upvotes: 3
Reputation: 2978
You can try to use re.sub inside a loop that iterates over myarray.items(). However, you'll have to do backslash first since otherwise that might replace things incorrectly. You also need to make sure that "{" and "}" happen first, so that you don't mix up the matching. Since dictionaries are unordered I suggest you use list of tuples instead:
def myfunc(h):
myarray = [
("\\","\\textbackslash")
("{","\\{"),
("}","\\}"),
("#","\\#"),
("$","\\$"),
("%","\\%"),
("&","\\&"),
("~","\\~{}"),
("_","\\_"),
("^","\\^{}")]
for (val, replacement) in myarray:
h = re.sub(val, replacement, h)
h = re.sub("\\textbackslash", "\\textbackslash{}", h)
return h
Upvotes: 1
Reputation: 21685
to escape metacharacters, use raw string and backslashes
r"regexp with a \* in it"
Upvotes: 0