本文整理汇总了C#中System.Text.RegularExpressions.Regex.CompileToAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.CompileToAssembly方法的具体用法?C# Regex.CompileToAssembly怎么用?C# Regex.CompileToAssembly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.CompileToAssembly方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
public class RegexCompilationTest
{
public static void Main()
{
RegexCompilationInfo expr;
List<RegexCompilationInfo> compilationList = new List<RegexCompilationInfo>();
// Define regular expression to detect duplicate words
expr = new RegexCompilationInfo(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant,
"DuplicatedString",
"Utilities.RegularExpressions",
true);
// Add info object to list of objects
compilationList.Add(expr);
// Define regular expression to validate format of email address
expr = new RegexCompilationInfo(@"^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[A-Z]{2,6}))$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant,
"EmailAddress",
"Utilities.RegularExpressions",
true);
// Add info object to list of objects
compilationList.Add(expr);
// Generate assembly with compiled regular expressions
RegexCompilationInfo[] compilationArray = new RegexCompilationInfo[compilationList.Count];
AssemblyName assemName = new AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null");
compilationList.CopyTo(compilationArray);
Regex.CompileToAssembly(compilationArray, assemName);
}
}
示例2: Main
//引入命名空间
using System;
using Utilities.RegularExpressions;
public class CompiledRegexUsage
{
public static void Main()
{
string text = "The the quick brown fox fox jumps over the lazy dog dog.";
DuplicatedString duplicateRegex = new DuplicatedString();
if (duplicateRegex.Matches(text).Count > 0)
Console.WriteLine("There are {0} duplicate words in \n '{1}'",
duplicateRegex.Matches(text).Count, text);
else
Console.WriteLine("There are no duplicate words in \n '{0}'",
text);
}
}
输出:
There are 3 duplicate words in 'The the quick brown fox fox jumps over the lazy dog dog.'
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
public class RegexCompilationTest
{
public static void Main()
{
RegexCompilationInfo expr;
List<RegexCompilationInfo> compilationList = new List<RegexCompilationInfo>();
// Define regular expression to detect duplicate words
expr = new RegexCompilationInfo(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant,
"DuplicatedString",
"Utilities.RegularExpressions",
true);
// Add info object to list of objects
compilationList.Add(expr);
// Define regular expression to validate format of email address
expr = new RegexCompilationInfo(@"^(?("")(""[^""]+?""@)|(([0-9A-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9A-Z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9A-Z][-\w]*[0-9A-Z]\.)+[zA-Z]{2,6}))$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant,
"EmailAddress",
"Utilities.RegularExpressions",
true);
// Add info object to list of objects
compilationList.Add(expr);
// Apply AssemblyTitle attribute to the new assembly
//
// Define the parameter(s) of the AssemblyTitle attribute's constructor
Type[] parameters = { typeof(string) };
// Define the assembly's title
object[] paramValues = { "General-purpose library of compiled regular expressions" };
// Get the ConstructorInfo object representing the attribute's constructor
ConstructorInfo ctor = typeof(System.Reflection.AssemblyTitleAttribute).GetConstructor(parameters);
// Create the CustomAttributeBuilder object array
CustomAttributeBuilder[] attBuilder = { new CustomAttributeBuilder(ctor, paramValues) };
// Generate assembly with compiled regular expressions
RegexCompilationInfo[] compilationArray = new RegexCompilationInfo[compilationList.Count];
AssemblyName assemName = new AssemblyName("RegexLib, Version=1.0.0.1001, Culture=neutral, PublicKeyToken=null");
compilationList.CopyTo(compilationArray);
Regex.CompileToAssembly(compilationArray, assemName, attBuilder);
}
}
示例4: Main
//引入命名空间
using System;
using System.Reflection;
using System.Text.RegularExpressions;
class MainClass {
public static void Main() {
RegexCompilationInfo[] regexInfo = new RegexCompilationInfo[2];
regexInfo[0] = new RegexCompilationInfo(@"^\d{4}$", RegexOptions.Compiled, "PinRegex", "", true);
regexInfo[1] = new RegexCompilationInfo(@"^\d{4}-?\d{4}-?\d{4}-?\d{4}$", RegexOptions.Compiled, "CreditCardRegex", "", true);
AssemblyName assembly = new AssemblyName();
assembly.Name = "MyRegEx";
Regex.CompileToAssembly(regexInfo, assembly);
}
}