namespace Active
{
using System;
using System.Reflection;
using System.Collections.Generic;
public class RegExp
{
object ComObject; Type ComType;
void Inint()
{
this.ComType = Type.GetTypeFromProgID("VBScript.RegExp");
this.ComObject = Activator.CreateInstance(this.ComType);
}
public RegExp()
{
this.Inint();
}
public RegExp(string pattern, bool global = true, bool ignoreCase = false, bool multiline = true)
{
this.Inint();
this.Pattern = pattern; this.Global = global;
this.IgnoreCase = ignoreCase; this.Multiline = multiline;
}
object DoMethod(object target, Type type, string name, object[] args, BindingFlags invokeAttr)
{
return type.InvokeMember(name, invokeAttr, null, target, args);
}
object DoMethod(string name, object[] args, BindingFlags invokeAttr)
{
return this.DoMethod(this.ComObject, this.ComType, name, args, invokeAttr);
}
object _invoke(string name, object[] args = null)
{
return this.DoMethod(this.ComObject, this.ComType, name, args, BindingFlags.InvokeMethod);
}
object _invoke(string name, object args)
{
return this._invoke(name, new object[] { args });
}
object _set(string name, object args)
{
return this.DoMethod(this.ComObject, this.ComType, name, new object[] { args }, BindingFlags.SetProperty);
}
object _get(string name)
{
return this.DoMethod(this.ComObject, this.ComType, name, null, BindingFlags.GetProperty);
}
public bool IgnoreCase
{
get { return (bool)this._get("IgnoreCase"); }
set { this._set("IgnoreCase", value); }
}
public bool Global
{
get { return (bool)this._get("Global"); }
set { this._set("Global", value); }
}
public string Pattern
{
get { return this._get("Pattern").ToString(); }
set { this._set("Pattern", value); }
}
public bool Multiline
{
get { return (bool)this._get("Multiline"); }
set { this._set("Multiline", value); }
}
public bool Test(string sourceString)
{
return (bool)this._invoke("Test", sourceString);
}
public MatchCollection Execute(string sourceString)
{
return new MatchCollection(this._invoke("Execute", sourceString));
}
public string Replace(string sourceString, string replaceVar)
{
return this._invoke("Replace", new object[] { sourceString, replaceVar }).ToString();
}
}
public class MatchCollection
{
Type ComType; object ComObject;
internal MatchCollection(object obj)
{
this.ComObject = obj; this.ComType = obj.GetType();
}
object _invoke(string name, object[] args = null)
{
return this.ComType.InvokeMember(name, BindingFlags.GetProperty, null, this.ComObject, args);
}
public int Count
{
get { return (int)this._invoke("Count"); }
}
public object NewEnum
{
get { return this._invoke("_NewEnum"); }
}
public Match this[int index]
{
get { return new Match(this._invoke("Item", new object[] { index })); }
}
public IEnumerator<Match> GetEnumerator()
{
for (var i = 0; i < this.Count; i++) {
yield return this[i];
}
}
}
public class Match
{
Type ComType; object ComObject;
internal Match(object obj)
{
this.ComObject = obj; this.ComType = obj.GetType();
}
object _get(string name)
{
return this.ComType.InvokeMember(name, BindingFlags.GetProperty, null, this.ComObject, null);
}
object _set(string name, object args)
{
return this.ComType.InvokeMember(name, BindingFlags.SetProperty, null, this.ComObject, new object[] { args });
}
public int FirstIndex
{
get {
var obj = this._get("FirstIndex");
return (int)(obj is string ? 0 : obj);
}
}
public int Length
{
get { return (int)this._get("Length"); }
}
public string Value
{
get { return this.ComObject is string ? this.ComObject.ToString() : this._get("Value").ToString(); }
}
public MatchCollection Groups
{
get { return new MatchCollection(this._get("SubMatches")); }
}
}
}
// PS:VB正则
var Regex = new RegExp(@"<\w+>(.*)</\w+>");
var Matchs = Regex.Execute("<a>四川</b>\r\n<c>成都</d>\r\n<e>安仁</f>");
foreach (var Match in Matchs) {
foreach (var Group in Match.Groups) {
System.Diagnostics.Debug.WriteLine(Group.Value);
}
}