Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 简化正则表达式以替换遵循某些子字符串逻辑的下划线_Python_Regex_String_Replace_Substring - Fatal编程技术网

Python 简化正则表达式以替换遵循某些子字符串逻辑的下划线

Python 简化正则表达式以替换遵循某些子字符串逻辑的下划线,python,regex,string,replace,substring,Python,Regex,String,Replace,Substring,输入字符串为: s = 'The\ue000 Project\ue000Gutenber g\ue000 E Book \ue000of\ue000 The\ue000 Ad vent ure s\ue000of\ue000 Sherlock\ue000 Holmes\n ' o = 'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n' 输出字符串为: s = 'The\ue00

输入字符串为:

s = 'The\ue000 Project\ue000Gutenber g\ue000 E Book \ue000of\ue000 The\ue000 Ad vent ure s\ue000of\ue000 Sherlock\ue000 Holmes\n '
o = 'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n'
输出字符串为:

s = 'The\ue000 Project\ue000Gutenber g\ue000 E Book \ue000of\ue000 The\ue000 Ad vent ure s\ue000of\ue000 Sherlock\ue000 Holmes\n '
o = 'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n'
请注意,在输入字符串中,
\ue000
是单词之间的硬分隔符

其目的是这样做:

  • [单元格28]:用下划线替换空格(表示两个非
    \ue000
    字符之间存在连接)

  • [单元格29]:如果存在
    \uE000
    序列,请删除下划线,因为以下划线结尾的前一个字符与下一个单词之间没有连接(请记住
    \uE000
    是硬词分隔符)

  • [单元格30]:然后将
    \ue000
    替换为一个空格,这样我们就可以看到带有下划线的单词,这些单词要么连接到单词的结尾字符,要么在两个空格之间悬挂下划线:

  • [单元格31]:对空格进行重复数据消除

  • [单元格32]:删除挂在两个空格之间的下划线

  • [单元格33]:现在单词的末尾有了下划线,我们可以安全地用
    \uu
    替换它们,以指示这两个子单词是可组合的

按上述顺序提供全套替换件:

text = text.replace(u'\n ', '\n')
text = text.replace(u' ', '_ ')
text = text.replace(u'_ \uE000', u' \uE000')
text = text.replace(u"\uE000", u' ')
text = text.replace(u'  ', u' ')
text = text.replace(u' _ ', u' ')
text = text.replace(u'_ ', u'_ _')
text = text.replace(u'  ', u' ')
注意:第一次替换
text.replace(u'\n','\n')
是必要的,因为字符串可能是全文文件,仅使用
str.strip()
不足以清除
\n
和新行之间不必要的空格


是否有一种不太复杂的方法来实现相同的输出字符串,从而保持以上述方式进行替换的逻辑?

我不太理解您关于换行符的倒数第二段,但除此之外,一个
re.sub()
就足够了:

>>> import re
>>> 
>>> re.sub(r'[ \ue000]+', lambda m: ' ' if '\ue000' in m.group() else '_ _', s)
'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n_ _'
这将查找
\ue000
和空格的所有序列,然后根据匹配是否包含
\ue000
使用返回空格或
'
的lambda替换这些序列

在那之后,据我所知(正如我所说,你倒数第二段有些混乱),你只需要去掉下划线和空格:

>>> re.sub(r'[ \ue000]+', lambda m: ' ' if '\ue000' in m.group() else '_ _', s).strip('_ ')
'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n'

我不太理解你倒数第二段关于换行符的内容,但除此之外,一个
re.sub()
就足以让你了解大部分内容:

>>> import re
>>> 
>>> re.sub(r'[ \ue000]+', lambda m: ' ' if '\ue000' in m.group() else '_ _', s)
'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n_ _'
这将查找
\ue000
和空格的所有序列,然后根据匹配是否包含
\ue000
使用返回空格或
'
的lambda替换这些序列

在那之后,据我所知(正如我所说,你倒数第二段有些混乱),你只需要去掉下划线和空格:

>>> re.sub(r'[ \ue000]+', lambda m: ' ' if '\ue000' in m.group() else '_ _', s).strip('_ ')
'The Project Gutenber_ _g E_ _Book of The Ad_ _vent_ _ure_ _s of Sherlock Holmes\n'

谢谢,不用担心新线。这可以在=)之前一步完成,顺便说一句,
r'[\s\ue000]+'
会更好吗?唯一的区别是使用
\s
将匹配所有空格(包括制表符和换行符)加上
\ue000
的序列-我不知道这是否匹配您的数据以及您想用它做什么,``明显优于
\s
只有空格键空格才重要,
\n
必须区别对待。感谢您的精彩回答!谢谢,不用担心新线。这可以在=)之前一步完成,顺便说一句,
r'[\s\ue000]+'
会更好吗?唯一的区别是使用
\s
将匹配所有空格(包括制表符和换行符)加上
\ue000
的序列-我不知道这是否匹配您的数据以及您想用它做什么,``明显优于
\s
只有空格键空格才重要,
\n
必须区别对待。感谢您的精彩回答!