-2

要匹配this is *some text*.但不匹配的正则表达式是什么this is \*another \*text。正则表达式应该匹配星号之间的文本。

4

1 回答 1

1
pattern = "\*(\w+(?:\s+\w+)*)\*"
re.findall(pattern, "this is *some text*.") // return 'some text'
re.findall(pattern, "this is \*another \*text") // return nothing

用“$”替换“*”:

subpattern = "(\*(\w+(?:\s+\w+)*)\*)"
re.sub(subpattern, r"$\2$", "this is *some text*.") // return 'this is $some text$.'
于 2012-05-27T13:11:58.267 回答