57

How to put together a regex that will match words that end with "Id" doing a case sensitive match?

1
  • 1
    What about these words: Id (starts and ends with Id) and O'HaraId (do you want to match O'HaraId or HaraId) and foo-barId (do you want to match foo-barId or barId)? In short: please define what a "word" means (or what you want it to be).
    – Bart Kiers
    Feb 12, 2010 at 20:11

6 Answers 6

104

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

3
  • @epitka, note that \w also matches numbers and the underscore. In short the strings ___Id and 12345Id will also be matched.
    – Bart Kiers
    Feb 12, 2010 at 20:17
  • I gave you an upvote, but epitka doesn't specify if just "Id" is allowable, so I'd be tempted to change the * for a + Feb 12, 2010 at 20:19
  • 1
    I found it doesn't match string that end with ID. you need to do \z at the end instead of \b
    – vitalyp
    Sep 2, 2021 at 20:49
9

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.

5

This may do the trick:

\b\p{L}*Id\b

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

2
  • does \p{L} work in C# regex? I've never seen that one before and usually opt for \w Feb 12, 2010 at 20:18
  • @BenAlabaster, yes: msdn.microsoft.com/en-us/library/… And yes, perhaps \w is sufficient for the OP, but it matches more than letters (see my comment under Gumbo's post).
    – Bart Kiers
    Feb 12, 2010 at 20:21
5

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

1
  • Why \z worked with me, while the \b mentioned in all other answers not working? Sep 8, 2019 at 10:05
4

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

2
  • 1
    The set a-z excludes é and other similar characters. Perhaps not an issue, but something epitka may want to know.
    – Bart Kiers
    Feb 12, 2010 at 20:18
  • 1
    [A-Za-z] doesn't match non-English alphabetic characters, so should be avoided in favour of \w unless a guarantee can be made that only English letters will appear. Feb 12, 2010 at 20:20
3
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.