我真的很感激你的时间。Python将多个捕获的REGEX匹配传递给函数
我成功地捕获了一些指定为* code *的分隔符标签之间的文本。我有多个IE:*代码*代码在这里#1 *代码*然后*代码*代码在这里#2 *代码*。我很努力地将REGEX捕获的代码在* code *标记之间传递给我的类进行格式化。它一遍又一遍地显示为“代码#1”。
The input text is:
*image1*
Some More Text here
That's a title pic and there are 2 more enable pictures per page.
*code* CENTER CODES HERE *code* Those can be a bit larger. And then there is more
code to show *code* MORE CENTER CODE *code*
Paragraph Test
这里是我已经捕获文本,然后遍历:
replace = CodeboxReplace()
codeboxRE = re.compile('\*code\*(.*?)\*code\*')
found = codeboxRE.findall(thisText)
for item in found:
thisText = codeboxRE.sub(replace(item), thisText)
确定,那么类CodeboxReplace()看起来像这样{CODEHERE}是我在的地方标记被替换实际代码这是在代码分隔符之间的匹配:
class CodeboxReplace(object):
def __init__(self):
self.counter = 0
def __call__(self, match):
self.counter += 1
.......some not relevant code here................
codeHereRE = re.compile('{CODEHERE}')
found = codeHereRE.findall(myCode)
for item in found:
myCode = codeHereRE.sub(match, myCode)
return myCode
所以,从根本上我想T的捕获的代码片段他用分隔符代替{CODEHERE}。但是每场比赛总是只使用REGEX的第一次捕捉。
帮助!谢谢!如果你想看到它呈现怎样看
: http://www.americantechnocracy.com/getArticle
最好的问候, 汤姆
这是不是一个真正的[MCVE(https://stackoverflow.com/help/mcve)。您正在省略可能会显示您想要的代码的部分内容。我认为你需要捕获第一个匹配'* code *(...)* code *'的匹配项,并用它来代替班级中不同文本中第一次出现的“{CODEHERE}”。这是真的? –