这是
替换它
我有以下字符串:
It reported the proportion of the edits made from America was 51% for the Wikipedia, and 25% for the simple Wikipedia.[142] The Wikimedia Foundation hopes to increase the number in the Global South to 37% by 2015.[143]
我试图用 .[xxx] n
替换每个字符lik this .[xxx]
;
x在这里是数字
我正在从不同的茎溢出答案中获得帮助;一个是:
python在字符的字符串中插入一个线断裂," x&quot"
REGEX:匹配fullstop和python中的一个单词
import re
str = "It reported the proportion of the edits made from America was 51%
for the Wikipedia, and 25% for the simple Wikipedia.[142] The Wikimedia
Foundation hopes to increase the number in the Global South to 37% by
2015.[143] "
x = re.sub(".[[0-9]{2,5}]s", ".[[0-9]{2,5}]sn",str)
print(x)
我期望以下输出:
It reported the proportion of the edits made from America was 51% for the Wikipedia, and 25% for the simple Wikipedia.[142]
The Wikimedia Foundation hopes to increase the number in the Global South to 37% by 2015.[143]”
但是我得到了:
It reported the proportion of the edits made from America was 51% for the Wikipedia, and 25% for the simple Wikipedia\.[[0-9]{2,5}]s The Wikimedia Foundation hopes to increase the number in the Global South to 37% by 2015\.[[0-9]{2,5}]s
您可以使用
(.[[^][]*])s*
并将其替换为1n
,请参见Regex101.com上的上的演示。
这是
(
.[ # ".[" literally
[^][]* # neither "[" nor "]" 0+ times
] # "]" literally
)s* # consume whitespaces, eventually
使用findall((识别匹配模式的列表。然后,您可以用原始字符串 ' n'
您可能想在re.sub
中使用捕获的组和反向引用。您也无需逃脱替换字符串( REGEX101 (:
import re
s = '''It reported the proportion of the edits made from America was 51% for the Wikipedia, and 25% for the simple Wikipedia.[142] The Wikimedia Foundation hopes to increase the number in the Global South to 37% by 2015.[143] '''
x = re.sub(r'.[([0-9]{2,5})]s', r'.[1] n', s)
print(x)
打印:
It reported the proportion of the edits made from America was 51% for the Wikipedia, and 25% for the simple Wikipedia.[142]
The Wikimedia Foundation hopes to increase the number in the Global South to 37% by 2015.[143]