[英]how to split the sentence into multiple sentence based on multiple condition regex?
I have the below sentences.我有以下句子。 I need to split the sentences into multiple sentences if the sentence has dot or a matched word.
如果句子有点或匹配的单词,我需要将句子分成多个句子。
Sentence 1: There was an error while trying to serialize parameter http://uri.org/:Message .句子 1:尝试序列化参数http://uri.org/:Message 时出错。 The InnerException message with data contract name 'enumStatus:' is not expected.
数据协定名称为“enumStatus:”的 InnerException 消息不是预期的。
Expected result:预期结果:
senetences = 1. There was an error while trying to serialize parameter http://uri.org/:vMessage.
2. The InnerException message with data contract name 'enumStatus:' is not expected.
Sentence 2: ORA-01756: quoted string not properly terminated ORA-06512: at module1, line 48 ORA-06512: at line 1句子 2:ORA-01756:引用的字符串没有正确终止 ORA-06512:在 module1,第 48 行 ORA-06512:在第 1 行
Expected result:预期结果:
senetences = 1. ORA-01756: quoted string not properly terminated
2. ORA-06512: at module1, line 48
3. ORA-06512: at line 1
I am using below regex to split the sentences.我使用下面的正则表达式来拆分句子。
sentences = re.split(r'(?<=\w\.)\s|ORA-[0-9]{1,8}', input)
Issue here is, for the first case, if any word followed by dot is working fine.这里的问题是,对于第一种情况,如果后跟点的任何单词工作正常。 For the second case, I am able to split the sentence.
对于第二种情况,我可以拆分句子。 I have 2 issues.
我有2个问题。
I need 3 sentences in this case.在这种情况下,我需要 3 个句子。
Any help would be really appreciated.任何帮助将非常感激。
You may use this regex for splitting:您可以使用此正则表达式进行拆分:
\s+(?=ORA-\d+)|(?<=\.)\s+(?=[A-Z])
RegEx Details:正则表达式详情:
\\s+(?=ORA-\\d+)
: Match 1+ whitespace if that is followed by ORA-
and 1+ digits \\s+(?=ORA-\\d+)
:如果后面跟着ORA-
和 1+ 数字,则匹配 1+ 个空格|
: OR (?<=\\.)\\s+(?=[AZ])
: Match 1+ whitespace if that is preceded by a dot and followed by an uppercase letter (?<=\\.)\\s+(?=[AZ])
:匹配 1+ 个空格,如果前面是一个点,后面跟一个大写字母Code:代码:
import re
arr = ["There was an error while trying to serialize parameter http://uri.org/:Message. The InnerException message with data contract name 'enumStatus:' is not expected.", "ORA-01756: quoted string not properly terminated ORA-06512: at module1, line 48 ORA-06512: at line 1"]
rx = re.compile(r'\s+(?=\bORA-\d+)|(?<=\.)\s+(?=[A-Z])')
for i in arr: print (rx.split(i))
Output:输出:
['There was an error while trying to serialize parameter http://uri.org/:Message.', "The InnerException message with data contract name 'enumStatus:' is not expected."]
['ORA-01756: quoted string not properly terminated', 'ORA-06512: at module1, line 48', 'ORA-06512: at line 1']
(?<=\w\.)\s|(ORA-[0-9]{1,8})
You can try this and replace by \\n\\1
.你可以试试这个并替换为
\\n\\1
。
See demo.见演示。
https://regex101.com/r/8yvUuZ/1/ https://regex101.com/r/8yvUuZ/1/
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<=\w\.)\s|(ORA-[0-9]{1,8})"
test_str = ("ORA-01756: quoted string not properly terminated ORA-06512: at module1, line 48 ORA-06512: at line 1\n"
"There was an error while trying to serialize parameter http://uri.org/:Message. The InnerException message with data contract name 'enumStatus:' is not expected.")
subst = "\\n\\1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.