用于匹配字符串的正则表达式

时间:2011-11-17 07:18:06

标签: c# regex winforms

什么是匹配C#中以下字符串中的单词callCALL的正则表达式?

NIFTY-CALL-1200-Aug11
NIFTY CALL 1200 Aug11
NIFTYCALL-CALL-1200-Aug11 //In this case second call word must be matched not NIFTYCALL.
NIFTYCALL CALL 1200 Aug11 //In this case second call word must be matched not NIFTYCALL.
CALLNIFTY CALL 1200 Aug11 //In this case second call word must be matched not CALLNIFTY.
CALLNIFTY CALL 1200 Aug11 //In this case second call word must be matched not CALLNIFTY.
CALLNIFTY Aug11 1200CALL //In this case last call word must be matched not CALLNIFTY.
CALLNIFTY 1200 Aug11CALL //In this case last call word must be matched not CALLNIFTY.

3 个答案:

答案 0 :(得分:2)

怎么样?
Regex regexObj = new Regex(@"(?:\b|[0-9])(CALL)\b", RegexOptions.Singleline);
  • (?:<b|[0-9])部分检查字边界或CALL之前的数字

  • CALL)找到字符串并将其放入匹配的组中

  • \b部分再次检查单词边界。

答案 1 :(得分:1)

这将是

Regex re = new Regex(@"(\d|\b)(CALL|call)(\d|\b)");

答案 2 :(得分:0)

还可以使用

Regex re = new Regex(@"(\d|\b)(CALL)(\d|\b)",RegexOptions.IgnoreCase);

而不是使用CALL |调用。这样,你也匹配“cAll”或“CALl”。 (如果需要的话)。