使用C#正则表达式匹配相关字符串
C#正则表达式匹配字符串的方法如下:
1.使用C#中使用正则表达式System.Text.RegularExpressions命名空间;
2.使用C#中使用正则表达式Matches()方法匹配字符串,格式如下:
MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
其中Str表示输入字符串,Pattern表示匹配模式,RegexOptions.IgnoreCase表示忽略大小写,RegexOptions.ExplicitCapture表示改变收集匹配方式。
3.匹配结果保存在MatchCollection集合中,可以通过循环遍历结果集取出Match对象的结果。
TestRagular.cs:
using System;
using System.Text.RegularExpressions;
namespace Magci.Test.Strings
{
public class TestRegular
{
public static void WriteMatches(string str, MatchCollection matches)
{
Console.WriteLine("\nString is : " + str);
Console.WriteLine("No. of matches : " + matches.Count);
foreach (Match nextMatch in matches)
{
//取出匹配字符串和最多10个外围字符
int Index = nextMatch.Index;
string result = nextMatch.ToString();
&
相关文档:
利用WM_COPYDATA在应用程序间传递数据很简单,开销也小
一、传递数据部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ThreeTorches
{
public struct Copydatastru ......
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过C#中的DllImport直接调用这些功能。
DllImport所在的名字空间 using System.Runtime.Inte ......
修饰符用于声明在外部实现的方法。extern 修饰符的常见用法是在使用 Interop 服务调入非
托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static,如下面的示例所示:[DllImport("avifil32.dll")]
private static extern void AVIFileInit();
注意
extern 关键字还可以定义外部程序集别名,使 ......
对于多态,还必需提一个C#中的关键字:new。前面提到,对于virtual方法,JIT会确定实例的实际类型然后决定调用什么方法。但是如果派生类中new关键字修饰方法,则它向CLR澄清此派生类中的方法与基类中的方法毫无关系,以下代码最终调用是基类的introduce方法:
Code
class Program
{
static vo ......