原生的Regex類操作較為復雜,所有花了一點時間進行封裝.
SimpleRegex.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexClass
{
class SimpleRegex
{
Match match;
string regexText; //正則運算式文本
string contentText; //被搜索的文本
/**
* SimpleRegex類建構式
*/
public SimpleRegex()
{
}
/**
* 創建本類物件
* @param 正則運算式文本
* @param 被搜索的文本
* @param 匹配模式 RegexOptions
*/
public void Create(String regexText, string contentText,RegexOptions options)
{
this.regexText = regexText;
this.contentText = contentText;
this.match = Regex.Match(contentText, regexText, options);
}
/**
* 取匹配數量
*/
public int getMatchNum()
{
return this.match.Groups.Count;
}
/**
* 取匹配結果
*/
public string getMatchText()
{
return this.match.Groups[0].Value;
}
/**
* 取子匹配結果 從1開始
* @param 子匹配文本索引
* 建議使用精益編程助手
*/
public string getChildMatchText(int index)
{
return this.match.Groups[index].Value;
}
/**
* 匹配下一個
*/
public void nextMatch()
{
this.match = this.match.NextMatch();
}
/**
* 當前是否匹配成功
*/
public bool isSuccess()
{
return this.match.Success;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263360.html
標籤:其他
