admin管理员组文章数量:1391925
I am trying to pick up python and as someone ing from Javascript I haven't really been able to understand python's regex package re
What I am trying to do is something I have done in javascript to build a very very simple templating "engine" (I understand AST is the way to go for anything more plex):
In javascript:
var rawString =
"{{prefix_HelloWorld}} testing this. {{_thiswillNotMatch}} \
{{prefix_Okay}}";
rawString.replace(
/\{\{prefix_(.+?)\}\}/g,
function(match, innerCapture){
return "One To Rule All";
});
In Javascript that will result in:
"One To Rule All testing this. {{_thiswillNotMatch}} One To Rule All"
And the function will get called twice with:
innerCapture === "HelloWorld"
match ==== "{{prefix_HelloWorld}}"
and:
innerCapture === "Okay"
match ==== "{{prefix_Okay}}"
Now, in python I have tried looking up docs on the re package
import re
Have tried doing something along the lines of:
match = re.search(r'pattern', string)
if match:
print match.group()
print match.group(1)
But it really doesn't make sense to me and doesn't work. For one, I'm not clear on what this group() concept means? And how am I to know if there is match.group(n)... group(n+11000)?
Thanks!
I am trying to pick up python and as someone ing from Javascript I haven't really been able to understand python's regex package re
What I am trying to do is something I have done in javascript to build a very very simple templating "engine" (I understand AST is the way to go for anything more plex):
In javascript:
var rawString =
"{{prefix_HelloWorld}} testing this. {{_thiswillNotMatch}} \
{{prefix_Okay}}";
rawString.replace(
/\{\{prefix_(.+?)\}\}/g,
function(match, innerCapture){
return "One To Rule All";
});
In Javascript that will result in:
"One To Rule All testing this. {{_thiswillNotMatch}} One To Rule All"
And the function will get called twice with:
innerCapture === "HelloWorld"
match ==== "{{prefix_HelloWorld}}"
and:
innerCapture === "Okay"
match ==== "{{prefix_Okay}}"
Now, in python I have tried looking up docs on the re package
import re
Have tried doing something along the lines of:
match = re.search(r'pattern', string)
if match:
print match.group()
print match.group(1)
But it really doesn't make sense to me and doesn't work. For one, I'm not clear on what this group() concept means? And how am I to know if there is match.group(n)... group(n+11000)?
Thanks!
Share Improve this question edited Jul 31, 2013 at 8:07 Blender 299k55 gold badges458 silver badges510 bronze badges asked Jul 31, 2013 at 7:53 John TomsonJohn Tomson 1,4792 gold badges14 silver badges16 bronze badges3 Answers
Reset to default 6Python's re.sub
function is just like JavaScript's String.prototype.replace
:
import re
def replacer(match):
return match.group(1).upper()
rawString = "{{prefix_HelloWorld}} testing this. {{_thiswillNotMatch}} {{prefix_Okay}}"
result = re.sub(r'\{\{prefix_(.+?)\}\}', replacer, rawString)
And the result:
'HELLOWORLD testing this. {{_thiswillNotMatch}} OKAY'
As for the groups, notice how your replacement function accepts a match
argument and an innerCapture
argument. The first argument is match.group(0)
. The second one is match.group(1)
.
I think you want to substitute all occurrences of {{prefix_*}} where * is basically anything. If so, this code works and is simple.
pattern = "\{\{prefix_.*?\}\}"
re.sub(pattern, "One To Rule All", rawString)
Cheers!
If you will be using the same pattern more than once (such as in a loop), then this is better:
pattern = re.pile("\{\{prefix_.*?\}\}")
# ... later ...
pattern.sub("One To Rule All", rawString)
本文标签: Python stringreplace equivalent (from Javascript)Stack Overflow
版权声明:本文标题:Python string.replace equivalent (from Javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744696256a2620294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论