Regex援助

Regex援助,regex,Regex,上面是我用来测试正则表达式的文本 我在PHP中使用它,只是试图提取不带前缀的字符串 本质上,我要寻找的是从给定前缀中提取这个字符串“字符串的这部分不应该更改,因为它包含note to self和NTS” 非常感谢您的帮助,谢谢 编辑:按请求信息已更改 对于RegEx在线测试,您可以使用以下内容:。 关于您的正则表达式:()-组符号。在这一主要群体中,你试图找到: \A(自我提醒) (NTS) 这里的OR由| 因此,要选择所需的所有内容,只需从搜索中删除\A-bell字符(\x07)((自我说明)

上面是我用来测试正则表达式的文本

我在PHP中使用它,只是试图提取不带前缀的字符串

本质上,我要寻找的是从给定前缀中提取这个字符串“字符串的这部分不应该更改,因为它包含note to self和NTS”

非常感谢您的帮助,谢谢

编辑:按请求信息已更改


对于RegEx在线测试,您可以使用以下内容:。 关于您的正则表达式:
()
-组符号。在这一主要群体中,你试图找到:

  • \A(自我提醒)
  • (NTS)
  • 这里的OR由
    |

    因此,要选择所需的所有内容,只需从搜索中删除
    \A
    -bell字符(
    \x07
    )((自我说明)|(NTS))。
    引用:

    \A
    是与字符串开头匹配的零宽度断言。您将要删除它

    您所需要的就是:

    Note To Self; This part of the string should not be changed because it contains note to self and NTS
    Note To Self ; This part of the string should not be changed because it contains note to self and NTS
    Note To Self : This part of the string should not be changed because it contains note to self and NTS
    Note To Self: This part of the string should not be changed because it contains note to self and NTS
    Note To Self - This part of the string should not be changed because it contains note to self and NTS
    Note To Self- This part of the string should not be changed because it contains note to self and NTS
    Note To Self This part of the string should not be changed because it contains note to self and NTS
    NTS ; This part of the string should not be changed because it contains note to self and NTS
    NTS; This part of the string should not be changed because it contains note to self and NTS
    NTS : This part of the string should not be changed because it contains note to self and NTS
    NTS: This part of the string should not be changed because it contains note to self and NTS
    NTS- This part of the string should not be changed because it contains note to self and NTS
    NTS - This part of the string should not be changed because it contains note to self and NTS
    NTS This part of the string should not be changed because it contains note to self and NTS
    This part of the string should not be changed because it contains note to self and NTS #NTS
    
    如果你不想和“红蚂蚁”比赛,你可以这样做:

    Note to self|NTS
    /Note to self|NTS/i                  for PHP (case insensitive)
    
    \b自我说明\b | \bNTS\b
    /\b PHP的self\b |\bNTS\b/i说明(不区分大小写)
    /\b自我提示\b | \b(?试试这个:

    \bNote to self\b|\bNTS\b
    /\bNote to self\b|\bNTS\b/i          for PHP (case insensitive)
    /\bNote to self\b|\b(?<!#)NTS\b/i    also ignores #NTS
    
    说明:

    • ^
      表示行的开头(支持的语言多于
      \A
    • #?
      查找0或1
      #
      符号
    • ([Nn]ote[Tt]o[Ss]elf | NTS)
      是一个or语句,查找
      [Nn]ote[Tt]o[Ss]elf
      NTS
      。括号中的
      [Nn]
      表示查找
      N
      N
      (允许您匹配“自我注释”和“自我注释”)
    • 结尾处的
      *
      仅与行的其余部分匹配:
      是任意字符,
      *
      是任意重复次数
    可能有用的参考资料:


    (即使您没有使用Python,我认为这些示例和解释也很有帮助)

    要删除
    Note To self
    NTS
    的所有实例(如果它们位于行的开头,可以选择后跟空格和/或标点),要删除
    #NTS
    的所有实例(如果它们位于行的末尾),您可以搜索

    ^#?([Nn]ote [Tt]o [Ss]elf|NTS).*
    
    什么都没有取代

    以PHP代码段的形式进行解释:

    (^(Note to self|NTS)\W*|#NTS$)
    

    \A
    不是bell字符,它是regex元字符(参见agent-j的答案)看起来我错了,但我只依赖于我答案中的第二个链接。对于Java和C,也没有使用这种组合。为什么最后两个示例不匹配呢?因为
    NTS
    /
    自我说明
    不在行的开头?这只是一个将关键短语放入字符串中的示例,我只想提取完整字符串中的“即使它包含NTS也不应该这样做”。这更有意义吗?我必须承认我有点困惑。您可以编辑您的帖子,以便我们能够准确地看到您的文件在处理后的样子吗?听起来这可以在单个正则表达式中完成。您还应该添加您正在使用的正则表达式引擎。已编辑:)-希望这能让事情更清楚一点。嗯,不是真的。转换前后的文本是什么样子的?谢谢,这在NTS部分非常有效,我如何使它不区分大小写?您使用的是什么正则表达式风格?C#?JavascriptIt将与PHP一起使用:)@Lewis,我添加了PHP示例。。。这使得它不敏感
    /regex/i
    (\b自我说明\b | \bNTS\b)\s?[|:|-]\s似乎工作得很好,现在我需要一种方法来获取结尾处的NTS。
    $result = preg_replace(
        '/(             # Either match...
         ^              #  start of line, followed by...
         (              #   either...
          Note to self  #   Note to self
         |              #   or
          NTS           #   NTS
         )              #  followed by...
         \W*            #  any number of non-alphanumeric characters
        |               # or
         \#NTS          #  match #NTS
         $              #  if it\'s the last thing before the end of the line
        )               # End of the alternation.
        /x', 
        '', $subject);