简体   繁体   English

以特定单词结尾的正则表达式

[英]Regex for ending with a specific word

I have following regex sandbox我有以下正则表达式沙箱

https://regex101.com/r/5rcScT/4 https://regex101.com/r/5rcScT/4

When I use \\w*-[Ff]iltered\\b it gets fff-filtered-qq and nope-Filtered-ok which I do not want.当我使用\\w*-[Ff]iltered\\b它会得到我不想要的fff-filtered-qqnope-Filtered-ok I only want words specifically ends with -filtered.我只希望单词特别以 -filtered 结尾。 Such as this-filtered and wow-Filtered比如this-filteredwow-Filtered

What I am missing here?我在这里缺少什么?

You may use a whitespace boundary (?!\\S) :您可以使用空白边界(?!\\S)

\w*-[Ff]iltered(?!\S)

See the regex demo查看正则表达式演示

To make sure you match at least 1 word char before a hyphen, replace * with + :为确保在连字符前至少匹配 1 个字符字符,请将*替换为+

\w+-[Ff]iltered(?!\S)

The (?!\\S) is a negative lookahead that fails the match if there is no non-whitespace char immediately to the right of the current location, ie it matches the location that is followed with whitespace or end of string (it is equal to the (?=\\s|$) positive lookahead, but is more efficient due to the absence of alternation). (?!\\S)是一个否定前瞻,如果当前位置右侧没有非空白字符,则匹配失败,即它匹配后面跟有空白或字符串结尾的位置(它等于到(?=\\s|$)正向前瞻,但由于没有交替,效率更高)。

You can use alteration with either a whitespace character or end of line (with /m flag):您可以使用带有空格字符或行尾的更改(带有/m标志):

/\w*-[Ff]iltered(\s|$)/m

Demo演示

This would be for a regex environment without a look around assertion (awk, ERE, etc) If you have a look around, use Wiktor Stribiżew's answer.这将适用于没有环顾断言(awk、ERE 等)的正则表达式环境 如果您环顾四周,请使用 Wiktor Stribiżew 的答案。

You can use this :你可以使用这个:

\w*Id\b

\\w* allows word characters in front of Id and the \\b ensures that Id is at the end of the word ( \\b is word boundary assertion). \\w*允许在Id前面有单词字符,而\\b确保Id在单词的末尾( \\b是单词边界断言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM