简体   繁体   中英

Regular Expression in C#

I want to write Regex in C#, I write the following code:

Regex reg = new Regex("n>");
string str = "burn";
int x=0;
if(reg.IsMatch(str))
  x++;

But, always x equals zero. does anyone know why?

Thanks, Rachel

您的意思是用>匹配单词边界,但这在.NET正则表达式中用\\b表示:

Regex reg = new Regex("n\\b");  // alternatively: Regex(@"n\b");

But, always x equals zero. does anyone know why?

Because the regular expression "n>" does not match the string "burn".

You are looking at the wrong part of the help. That help is for Visual Studio's Find and Replace dialogs. Some of the syntax from help is not valid for use in C# (or rather does not have the same effect). So if you had a word "burn" in your code and you entered n> into find box and tried searching you would've found n in burn - see the picture:

屏幕截图

You probably need to use n\\b as Tomalak pointed out.

See more details from MSDN: Regular Expression Language Elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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