如何在C#中使用正则表达式检索选中的文本? [英] How do you retrieve selected text using Regex in C#?

查看:102
本文介绍了如何在C#中使用正则表达式检索选中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么了检索C#中使用正则表达式选中的文字



我要寻找的C#代码,相当于这个Perl代码:

  $ indexVal = 0; 
如果($字符串=〜/索引:(\d *)/){$ indexVal = $ 1;}


解决方案

  INT indexVal = 0; 
正则表达式重新=新的正则表达式(@指数:(\d *))
匹配M = re.Match(S)

如果(m.Success)
indexVal = int.TryParse(m.Groups [1]的ToString());



我可能有组号码错了,但你应该可以从这里找到答案。


How do you retrieve selected text using Regex in C#?

I am looking for C# code that is equivalent to this Perl code:

$indexVal = 0;
if($string =~ /Index: (\d*)/){$indexVal = $1;}

解决方案

int indexVal = 0;
Regex re = new Regex(@"Index: (\d*)")
Match m = re.Match(s)

if(m.Success)
  indexVal = int.TryParse(m.Groups[1].toString());

I might have the group number wrong, but you should be able to figure it out from here.

这篇关于如何在C#中使用正则表达式检索选中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆