简体   繁体   English

正则表达式匹配不包括下划线的字符串

[英]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? 我可以使用Regex.Match()方法执行此检查吗?

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! 提前致谢!

No, regular expressions aren't the right tool. 不,正则表达式不是正确的工具。 You would have to do something equivalent to _*m_*y_*t_*a_*b_*l_*e_* . 您将必须执行与_*m_*y_*t_*a_*b_*l_*e_*等价的操作。 Clearly that's not a good idea. 显然,这不是一个好主意。 Try: 尝试:

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

You don't need to use regular expressions. 您不需要使用正则表达式。

Instead, you can call Replace : 相反,您可以调用Replace

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

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

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