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

本文关键字:正则表达式 字符串 用于 | 更新日期: 2023-09-27 18:15:01

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

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

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

  • (CALL)查找字符串并将其放入匹配组

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

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

也可以使用

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

而不是使用CALL| CALL。这样,你也会匹配"cAll"或"cAll"。(当然,如果需要的话)