NotePad ++使用REGEX Pattern of Delimiters获取单词


2

我目前正在使用此REGEX来获取前缀为PRE_的整个单词

\b(PRE_)\S*

这适用于大多数情况,但我想处理特殊字符是单词的一部分时的情况,例如句号,逗号或其他特殊字符。,; - {}()[]

例如,这里的话:

PRE_samplewordwithoutdelimiter
PRE_sampleword.otherwordsnotincluded;
PRE_Sampleword{}...deleted
PRE_complexword()a.;.is deleted
Somewords ahead PRE_sometext() ending in other words
Words with bracket [PRE_brackettext] are deleted
PRE_sampleword is spaced out so deleted
sampleword.PRE_deleted;
notdeleted.notdeleted.PRE_

我只希望找到一个分隔词的第一部分。所以我可以删除或替换这个词。因此,在使用“”作为文本替换此场景中的所有PRE_前缀单词的情况下,我会得到:

<DELETED>
<DELETED>.otherwordsnotincluded;
<DELETED>{}...deleted
<DELETED>()a.;.is deleted
Somewords ahead <DELETED>() ending in other words
Words with bracket [<DELETED>] are deleted
<DELETED> is spaced out so deleted
sampleword.<DELETED>;
notdeleted.notdeleted.<DELETED>

我正在尝试不同的REGEX,但在整个样本中没有任何真正匹配完全正确。像下面这样的东西不起作用:

\b(PRE_)\S*(?:[;]|[.][-])$

这里的任何帮助将不胜感激。

Answers:


1

方法1

不要在之后包含所有非空格字符(\S在你的第一个正则表达式中)PRE_,只需在搜索中声明所有“非分隔符”,它应该这样做。

以下适用于您的所有示例,通过替换\S[A-Za-z]

\bPRE_[A-Za-z]*

如果您希望包含数字,连字符(-)和下划线(_),则可以使用以下代码:

\bPRE_[-A-Za-z0-9_]*

方法2

否则,你可以修改\S为“任何事情,但\s和其他分隔符,如.,;{}()[](在你希望的任何人)”这样一来你的正则表达式变

\bPRE_[^.;,{}()[\]\s]*

这句话[^blahblah]意味着除了blahblah之外的任何角色。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.