Python 删除两个正则表达式特殊字符分隔符之间的文本

Python 删除两个正则表达式特殊字符分隔符之间的文本,python,Python,在其中,我想删除分隔符^^和^^之间的文本 输出应为“重要信息Imp Info”您可以使用正则表达式执行此操作: s = '^^^@ """@$ raw data &*823ohcneuj^^^ Important Information ^^^raw data^^^ Imp Info' 此正则表达式中的关键元素是: 转义^字符,因为它有特殊含义 使用*? 这将打印“重要信息Imp Info”。谢谢,我尝试了相同的方法,但没有应用“\”,因此无法获得输出。 import re s =

在其中,我想删除分隔符^^和^^之间的文本


输出应为“重要信息Imp Info”

您可以使用正则表达式执行此操作:

s = '^^^@ """@$ raw data &*823ohcneuj^^^ Important Information ^^^raw data^^^ Imp Info' 
此正则表达式中的关键元素是:

  • 转义
    ^
    字符,因为它有特殊含义
  • 使用
    *?

  • 这将打印“重要信息Imp Info”。

    谢谢,我尝试了相同的方法,但没有应用“\”,因此无法获得输出。
    import re
    s = '^^^@ """@$ raw data &*823ohcneuj^^^ Important Information ^^^raw data^^^ Imp Info'
    important = re.compile(r'\^\^\^.*?\^\^\^').sub('', s)
    
    def removeText(text):
         carrotCount = 0
         newText = ""
         for char in text: 
               if(char == '^'):
                     # Reset if we have exceeded 2 sets of carrots
                     if(carrotCount == 6):
                          carrotCount = 1
                     else:
                         carrotCount += 1
               # Check if we have reached the first '^^^'
               elif(carrotCount == 3):
                    # Ignore everything between the carrots
                    if(char != '^'):
                         continue;
                    # Add the second set of carrots when we find them
                    else:
                         carrotCount += 1
               # Check if we have reached the end of the second ^^^
               # If we have, we have the message
               elif(carrotCount == 6):
                    newText += char
          return newText