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
以上是我用来测试我的正则表达式的文本。
我在PHP中使用它,并且只是尝试提取没有前缀的字符串。
基本上,我正在寻找的是提取这个字符串'不应该更改字符串的这一部分,因为它包含来自给定前缀的自我和NTS的注释。
非常感谢您的帮助,谢谢!
编辑:按要求信息已更改。
答案 0 :(得分:1)
要移除Note to self
或NTS
的所有实例(如果它们位于该行的开头,可选地后跟空格和/或标点符号),并删除#NTS
如果它位于一行结束,你可以搜索
(^(Note to self|NTS)\W*|#NTS$)
并替换为零。
PHP代码片段形式的说明:
$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);
答案 1 :(得分:0)
要在线测试RegEx,您可以使用:http://gskinner.com/RegExr/。
关于您的RegEx:()
- 组符号。在这个主要群体中,你试图找到:
\A(Note to self)
(NTS)
这里的OR是|
因此,要选择所有需要,请从搜索\A
中删除\x07
- 铃声字符(((Note to self)|(NTS))
)。
参考:http://www.regular-expressions.info/reference.html
答案 2 :(得分:0)
\A
是一个零宽度断言,匹配字符串的开头。你想删除它。
所有你需要的是:
Note to self|NTS
/Note to self|NTS/i for PHP (case insensitive)
虽然如果你不想匹配“RED ANTS”,你可以这样做:
\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
答案 3 :(得分:0)
试试这个:
^#?([Nn]ote [Tt]o [Ss]elf|NTS).*
说明:
^
表示该行的开头(支持的语言多于\A
)#?
查找0或1个#
符号([Nn]ote [Tt]o [Ss]elf|NTS)
是一个或声明,正在寻找[Nn]ote [Tt]o [Ss]elf
或NTS
。括号中的[Nn]
表示找到N
或n
(允许您匹配“自我注释”和“自我注释”).*
最后只与该行的其余部分匹配:.
是任意字符,*
是任意数量的重复。可能有用的参考资料:
http://www.regular-expressions.info/refflavors.html
http://docs.python.org/library/re.html(即使您没有使用Python,我认为示例和解释也很有帮助)