【问题标题】:how to split the sentence into multiple sentence based on multiple condition regex?如何根据多个条件正则表达式将句子拆分为多个句子?
【发布时间】:2021-07-12 09:59:49
【问题描述】:

我有以下句子。如果句子有点或匹配的单词,我需要将句子分成多个句子。

句子 1:尝试序列化参数 http://uri.org/:Message 时出错。不应出现数据协定名称为“enumStatus:”的 InnerException 消息。

预期结果:

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.
                        

句子 2:ORA-01756:引用的字符串未正确终止 ORA-06512:在模块 1 第 48 行 ORA-06512:在第 1 行

预期结果:

senetences = 1. ORA-01756: quoted string not properly terminated
             2. ORA-06512: at module1, line 48
             3. ORA-06512: at line 1
                        

我正在使用下面的正则表达式来拆分句子。

 sentences = re.split(r'(?<=\w\.)\s|ORA-[0-9]{1,8}', input)
 

这里的问题是,对于第一种情况,如果任何单词后跟点都可以正常工作。 对于第二种情况,我可以拆分句子。我有 2 个问题。

  1. 它正在删除整个匹配词“ORA-”。但我需要整个词。
  2. 我得到的是 4 个句子而不是 3 个句子。
    1. (first 为空,因为它有起始词 ORA-)
    2. 引用的字符串未正确终止
    3. 在模块 1 第 48 行
    4. 在 1 号线

在这种情况下我需要 3 句话。

任何帮助将不胜感激。

【问题讨论】:

    标签: python regex string


    【解决方案1】:

    您可以使用此正则表达式进行拆分:

    \s+(?=ORA-\d+)|(?<=\.)\s+(?=[A-Z])
    

    RegEx Demo

    正则表达式详细信息:

    • \s+(?=ORA-\d+):匹配 1+ 个空格,如果后面跟着 ORA- 和 1+ 个数字
    • |:或者
    • (?&lt;=\.)\s+(?=[A-Z]): 匹配 1+ 个空格,如果它前面有一个点,后面是一个大写字母

    Code Demo

    代码:

    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))
    

    输出:

    ['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']
    

    【讨论】:

      【解决方案2】:
      (?<=\w\.)\s|(ORA-[0-9]{1,8})
      

      您可以试试这个并替换为\n\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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-24
        • 2016-08-04
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        相关资源
        最近更新 更多