C#中正则表达式的高级应用

2019-08-29 22:02

C#中正则表达式的高级应用

1。在正则表达式中定义变量并调用:

using System;

using System.Text.RegularExpressions;

public class Test {

public static void Main () {

// Define a regular expression for repeated words.

Regex rx = new Regex(@\, RegexOptions.Compiled | RegexOptions.IgnoreCase);

// Define a test string.

string text = \;

// Find matches.

MatchCollection matches = rx.Matches(text);

// Report the number of matches found.

Console.WriteLine(\, matches.Count);

// Report on each match.

foreach (Match match in matches) {

string word = match.Groups[\].Value; int index = match.Index;

Console.WriteLine(\, word, index); } } }

其中?定义了一个变量,之后的\\k调用自身定义的变量word。

2。好用的Regex.Replace 和Match.Result

这个例子实现输入的日期更改格式的功能,用正则表达式自动搜索字符串并替换,注意正则表达式中变量的使用。

public string MDYToDMY(string input) {

return Regex.Replace(input,

\, \); }

Match.Result是返回一个可以带正则表达式中变量值的字符串。

public string Extension(string url) {

Regex r = new Regex(@\, RegexOptions.Compiled);

return r.Match(url).Result(\); }


C#中正则表达式的高级应用.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:二年级举一反三试题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: