Nick
Nick

Reputation: 19664

RegEx to match on string excluding underscores

I need to check if two strings match. The first string will not contain underscores the other will. Removing the underscores from the second string would result in the strings being equivalent. Can I perform this check using the Regex.Match() method?

Here is an example of what I'm looking for:

my_table == mytable;
db_rv_term == dbrvterm;

So I just want to match the two strings excluding the underscores.

Thanks in advance!

Upvotes: 1

Views: 920

Answers (2)

SLaks
SLaks

Reputation: 887453

You don't need to use regular expressions.

Instead, you can call Replace:

if (str1.Replace("_", "") == str2)

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361615

No, regular expressions aren't the right tool. You would have to do something equivalent to _*m_*y_*t_*a_*b_*l_*e_*. Clearly that's not a good idea. Try:

if (str1 == str2.Replace("_", ""))

Upvotes: 5

Related Questions