C# 正则表达式:匹配以“Id”结尾的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:47:32  来源:igfitidea点击:

regex: match word that ends with "Id"

c#regex

提问by epitka

I need help putting together a regex that will match word that ends with "Id" with case sensitive match.

我需要帮助整理一个正则表达式,它将匹配以“Id”结尾的单词并区分大小写。

采纳答案by Gumbo

Try this regular expression:

试试这个正则表达式:

\w*Id\b

\w*allows word characters in front of Idand the \bensures that Idis at the end of the word (\bis word boundary assertion).

\w*允许在单词前面的字符,Id\b确保Id在单词的末尾(\b是单词边界断言)。

回答by TK-421

How about \A[a-z]*Id\z? [This makes characters before Idoptional. Use \A[a-z]+Id\zif there needs to be one or more characters preceding Id.]

怎么样\A[a-z]*Id\z?[这使得Id可选的字符之前。使用\A[a-z]+Id\z,如果需要有之前的一个或多个字符Id。]

回答by James Curran

Regex ids = new Regex(@"\w*Id\b", RegexOptions.None);

"\b" means "word break" & \w mean any word character, so \w*Id\b means "{stuff}Id". By not including RegexOptions.IgnoreCase, it will be case sensitive.

“\b”表示“断字”,\w 表示任何单词字符,所以\w*Id\b 表示“{stuff}Id”。通过不包括 RegexOptions.IgnoreCase,它将区分大小写。

回答by Squidly

I would use
\b[A-Za-z]*Id\b
The \b matches the beginning and end of a word i.e. space, tab or newline, or the beginning or end of a string.

我会使用
\b[A-Za-z]*Id\b
\b 匹配单词的开头和结尾,即空格、制表符或换行符,或者字符串的开头或结尾。

The [A-Za-z] will match any letter, and the * means that 0+ get matched. Finally there is the Id.

[A-Za-z] 将匹配任何字母,* 表示匹配 0+。最后是Id。

Note that this will match words that have capital letters in the middle such as 'teStId'.

请注意,这将匹配中间有大写字母的单词,例如“teStId”。

I use http://www.regular-expressions.info/for regex reference

我使用http://www.regular-expressions.info/作为正则表达式参考

回答by Bart Kiers

This may do the trick:

这可能会奏效:

\b\p{L}*Id\b

Where \p{L}matches any (Unicode) letter and \bmatches a word boundary.

其中\p{L}匹配任何(Unicode)字母并\b匹配单词边界。

回答by BenAlabaster

Gumbo gets my vote, however, the OP doesn't specify whether just "Id" is an allowable word, which means I'd make a minor modification:

Gumbo 得到了我的投票,但是,OP 没有指定是否只是“Id”是一个允许的词,这意味着我会做一个小的修改:

\w+Id\b

1 or more word characters followed by "Id" and a breaking space. The [a-zA-Z] variants don't take into account non-English alphabetic characters. I might also use \s instead of \b as a space rather than a breaking space. It would depend if you need to wrap over multiple lines.

1 个或多个单词字符后跟“Id”和一个分隔空格。[a-zA-Z] 变体不考虑非英文字母字符。我也可能使用 \s 而不是 \b 作为空格而不是中断空格。这取决于您是否需要换行多行。