[英]How to add newline after every characters like ” .[xxx] “ in a string in python
I have the following string : 我有以下字符串:
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]
I am trying to replace every characters lik this .[xxx]
with .[xxx] \\n
; 我试图用
.[xxx] \\n
替换所有喜欢此.[xxx]
字符。
x are digits here x是数字
I am taking help from different stalk overflow answers; 我正在从不同的茎溢出答案中寻求帮助; one such is :
其中之一是:
Python insert a line break in a string after character "X" Python在字符“ X”后的字符串中插入换行符
Regex: match fullstop and one word in python 正则表达式:匹配句号和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}\]\s\n",str)
print(x)
I expect the following output: 我期望以下输出:
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]”
But I am getting: 但我得到:
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
You probably want to use capturing groups and back-referrences in re.sub
. 您可能想在
re.sub
使用捕获组和反向引用。 You also don't need to escape the replacement string ( regex101 ): 您也不需要转义替换字符串( 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)
Prints: 打印:
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]
You may use 您可以使用
(\.\[[^][]*\])\s*
And replace this with \\1\\n
, see a demo on regex101.com . 并将其替换为
\\1\\n
,请参阅regex101.com上的演示 。
( \\.\\[ # ".[" literally [^][]* # neither "[" nor "]" 0+ times \\] # "]" literally )\\s* # consume whitespaces, eventually
Use findall() to identify list of matching patterns. 使用findall()识别匹配模式的列表。 Then you can replace it with original string+'\\n'
然后,您可以将其替换为原始字符串+'\\ n'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.