Regex: match word that ends with "Id"

C#Regex

C# Problem Overview


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

C# Solutions


Solution 1 - C#

Try this regular expression:

\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).

Solution 2 - C#

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:

\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.

Solution 3 - C#

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

Solution 4 - C#

This may do the trick:

\b\p{L}*Id\b

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

Solution 5 - C#

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.

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

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

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

Solution 6 - C#

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionepitkaView Question on Stackoverflow
Solution 1 - C#GumboView Answer on Stackoverflow
Solution 2 - C#BenAlabasterView Answer on Stackoverflow
Solution 3 - C#TK-421View Answer on Stackoverflow
Solution 4 - C#Bart KiersView Answer on Stackoverflow
Solution 5 - C#SquidlyView Answer on Stackoverflow
Solution 6 - C#James CurranView Answer on Stackoverflow