当前位置: 代码网 > it编程>编程语言>Asp.net > .NET正则表达式的最佳用法

.NET正则表达式的最佳用法

2024年05月19日 Asp.net 我要评论
.net 中的正则表达式引擎是一种功能强大而齐全的工具,它基于模式匹配(而不是比较和匹配文本)来处理文本。 在大多数情况下,它可以快速、高效地执行模式匹配。 但在某些情况下,正则表达式引擎的速度似乎很

.net 中的正则表达式引擎是一种功能强大而齐全的工具,它基于模式匹配(而不是比较和匹配文本)来处理文本。 在大多数情况下,它可以快速、高效地执行模式匹配。 但在某些情况下,正则表达式引擎的速度似乎很慢。 在极端情况下,它甚至看似停止响应,因为它会用若干个小时甚至若干天处理相对小的输入。

本主题概述开发人员为了确保其正则表达式实现最佳性能可以采纳的一些最佳做法。

考虑输入源

通常,正则表达式可接受两种类型的输入:受约束的输入或不受约束的输入。 受约束的输入是源自已知或可靠的源并遵循预定义格式的文本。 不受约束的输入是源自不可靠的源(如 web 用户)并且可能不遵循预定义或预期格式的文本。

编写的正则表达式模式的目的通常是匹配有效输入。 也就是说,开发人员检查他们要匹配的文本,然后编写与其匹配的正则表达式模式。 然后,开发人员使用多个有效输入项进行测试,以确定此模式是否需要更正或进一步细化。 当模式可匹配所有假定的有效输入时,则将其声明为生产就绪并且可包括在发布的应用程序中。 这使得正则表达式模式适合匹配受约束的输入。 但它不适合匹配不受约束的输入。

若要匹配不受约束的输入,正则表达式必须能够高效处理以下三种文本:

  • 与正则表达式模式匹配的文本。

  • 与正则表达式模式不匹配的文本。

  • 与正则表达式模式大致匹配的文本。

对于为了处理受约束的输入而编写的正则表达式,最后一种文本类型尤其存在问题。 如果该正则表达式还依赖大量回溯,则正则表达式引擎可能会花费大量时间(在有些情况下,需要许多个小时或许多天)来处理看似无害的文本。

例如,考虑一种很常用但很有问题的用于验证电子邮件地址别名的正则表达式。 编写正则表达式 ^[0-9a-z]([-.\w]*[0-9a-z])*$ 的目的是处理被视为有效的电子邮件地址,该地址包含一个字母数字字符,后跟零个或多个可为字母数字、句点或连字符的字符。 该正则表达式必须以字母数字字符结束。 但正如下面的示例所示,尽管此正则表达式可以轻松处理有效输入,但在处理接近有效的输入时性能非常低效。

using system;
using system.diagnostics;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      stopwatch sw;
      string[] addresses = { "aaaaaaaaaaa@contoso.com",
                             "aaaaaaaaaaaaaaaaaaaa!@contoso.com" };
      // the following regular expression should not actually be used to
      // validate an email address.
      string pattern = @"^[0-9a-z]([-.\w]*[0-9a-z])*$";
      string input;

      foreach (var address in addresses) {
         string mailbox = address.substring(0, address.indexof("@"));
         int index = 0;
         for (int ctr = mailbox.length - 1; ctr >= 0; ctr--) {
            index++;

            input = mailbox.substring(ctr, index);
            sw = stopwatch.startnew();
            match m = regex.match(input, pattern, regexoptions.ignorecase);
            sw.stop();
            if (m.success)
               console.writeline("{0,2}. matched '{1,25}' in {2}",
                                 index, m.value, sw.elapsed);
            else
               console.writeline("{0,2}. failed  '{1,25}' in {2}",
                                 index, input, sw.elapsed);
         }
         console.writeline();
      }
   }
}

// the example displays output similar to the following:
//     1. matched '                        a' in 00:00:00.0007122
//     2. matched '                       aa' in 00:00:00.0000282
//     3. matched '                      aaa' in 00:00:00.0000042
//     4. matched '                     aaaa' in 00:00:00.0000038
//     5. matched '                    aaaaa' in 00:00:00.0000042
//     6. matched '                   aaaaaa' in 00:00:00.0000042
//     7. matched '                  aaaaaaa' in 00:00:00.0000042
//     8. matched '                 aaaaaaaa' in 00:00:00.0000087
//     9. matched '                aaaaaaaaa' in 00:00:00.0000045
//    10. matched '               aaaaaaaaaa' in 00:00:00.0000045
//    11. matched '              aaaaaaaaaaa' in 00:00:00.0000045
//
//     1. failed  '                        !' in 00:00:00.0000447
//     2. failed  '                       a!' in 00:00:00.0000071
//     3. failed  '                      aa!' in 00:00:00.0000071
//     4. failed  '                     aaa!' in 00:00:00.0000061
//     5. failed  '                    aaaa!' in 00:00:00.0000081
//     6. failed  '                   aaaaa!' in 00:00:00.0000126
//     7. failed  '                  aaaaaa!' in 00:00:00.0000359
//     8. failed  '                 aaaaaaa!' in 00:00:00.0000414
//     9. failed  '                aaaaaaaa!' in 00:00:00.0000758
//    10. failed  '               aaaaaaaaa!' in 00:00:00.0001462
//    11. failed  '              aaaaaaaaaa!' in 00:00:00.0002885
//    12. failed  '             aaaaaaaaaaa!' in 00:00:00.0005780
//    13. failed  '            aaaaaaaaaaaa!' in 00:00:00.0011628
//    14. failed  '           aaaaaaaaaaaaa!' in 00:00:00.0022851
//    15. failed  '          aaaaaaaaaaaaaa!' in 00:00:00.0045864
//    16. failed  '         aaaaaaaaaaaaaaa!' in 00:00:00.0093168
//    17. failed  '        aaaaaaaaaaaaaaaa!' in 00:00:00.0185993
//    18. failed  '       aaaaaaaaaaaaaaaaa!' in 00:00:00.0366723
//    19. failed  '      aaaaaaaaaaaaaaaaaa!' in 00:00:00.1370108
//    20. failed  '     aaaaaaaaaaaaaaaaaaa!' in 00:00:00.1553966
//    21. failed  '    aaaaaaaaaaaaaaaaaaaa!' in 00:00:00.3223372

如该示例输出所示,正则表达式引擎处理有效电子邮件别名的时间间隔大致相同,与其长度无关。 另一方面,当接近有效的电子邮件地址包含五个以上字符时,字符串中每增加一个字符,处理时间会大约增加一倍。 这意味着,处理接近有效的 28 个字符构成的字符串将需要一个小时,处理接近有效的 33 个字符构成的字符串将需要接近一天的时间。

由于开发此正则表达式时只考虑了要匹配的输入的格式,因此未能考虑与模式不匹配的输入。 这反过来会使与正则表达式模式近似匹配的不受约束输入的性能显著降低。

若要解决此问题,可执行下列操作:

  • 开发模式时,应考虑回溯对正则表达式引擎的性能的影响程度,特别是当正则表达式设计用于处理不受约束的输入时。 有关详细信息,请参阅控制回溯部分。

  • 使用无效输入、接近有效的输入以及有效输入对正则表达式进行完全测试。 若要为特定正则表达式随机生成输入,可以使用 rex,这是 microsoft research 提供的正则表达式探索工具。

适当处理对象实例化

.net 正则表达式对象模型的核心是 xref:system.text.regularexpressions.regex?displayproperty=namewithtype 类,表示正则表达式引擎。 通常,影响正则表达式性能的单个最大因素是 xref:system.text.regularexpressions.regex 引擎的使用方式。 定义正则表达式需要将正则表达式引擎与正则表达式模式紧密耦合。 无论该耦合过程是需要通过向其构造函数传递正则表达式模式来实例化 xref:system.text.regularexpressions.regex 还是通过向其传递正则表达式模式和要分析的字符串来调用静态方法,都必然会消耗大量资源。

可将正则表达式引擎与特定正则表达式模式耦合,然后使用该引擎以若干种方式匹配文本:

  • 可以调用静态模式匹配方法,如 xref:system.text.regularexpressions.regex.match(system.string%2csystem.string)?displayproperty=namewithtype。 这不需要实例化正则表达式对象。

  • 可以实例化一个 xref:system.text.regularexpressions.regex 对象并调用已解释的正则表达式的实例模式匹配方法。 这是将正则表达式引擎绑定到正则表达式模式的默认方法。 如果实例化 xref:system.text.regularexpressions.regex 对象时未使用包括 options 标记的 xref:system.text.regularexpressions.regexoptions.compiled 自变量,则会生成此方法。

  • 可以实例化一个 xref:system.text.regularexpressions.regex 对象并调用已编译的正则表达式的实例模式匹配方法。 当使用包括 xref:system.text.regularexpressions.regex 标记的 options 参数实例化 xref:system.text.regularexpressions.regexoptions.compiled 对象时,正则表达式对象表示已编译的模式。

  • 可以创建一个与特定正则表达式模式紧密耦合的特殊用途的 xref:system.text.regularexpressions.regex 对象,编译该对象,并将其保存到独立程序集中。 为此,可调用 xref:system.text.regularexpressions.regex.compiletoassembly*?displayproperty=namewithtype 方法。

这种调用正则表达式匹配方法的特殊方式会对应用程序产生显著影响。 以下各节讨论何时使用静态方法调用、已解释的正则表达式和已编译的正则表达式,以改进应用程序的性能。

静态正则表达式

建议将静态正则表达式方法用作使用同一正则表达式重复实例化正则表达式对象的替代方法。 与正则表达式对象使用的正则表达式模式不同,静态方法调用所使用的模式中的操作代码或已编译的 microsoft 中间语言 (msil) 由正则表达式引擎缓存在内部。

例如,事件处理程序会频繁调用其他方法来验证用户输入。 下面的代码中反映了这一点,其中一个 xref:system.windows.forms.button 控件的 xref:system.windows.forms.control.click 事件用于调用名为 isvalidcurrency 的方法,该方法检查用户是否输入了后跟至少一个十进制数的货币符号。

public void okbutton_click(object sender, eventargs e)
{
   if (! string.isnullorempty(sourcecurrency.text))
      if (regexlib.isvalidcurrency(sourcecurrency.text))
         performconversion();
      else
         status.text = "the source currency value is invalid.";
}

下面的示例显示 isvalidcurrency 方法的一个非常低效的实现。 请注意,每个方法调用使用相同模式重新实例化 xref:system.text.regularexpressions.regex 对象。 这反过来意味着,每次调用该方法时,都必须重新编译正则表达式模式。

using system;
using system.text.regularexpressions;

public class regexlib
{
   public static bool isvalidcurrency(string currencyvalue)
   {
      string pattern = @"\p{sc}+\s*\d+";
      regex currencyregex = new regex(pattern);
      return currencyregex.ismatch(currencyvalue);
   }
}

应将此低效代码替换为对静态 xref:system.text.regularexpressions.regex.ismatch(system.string%2csystem.string)?displayproperty=namewithtype 方法的调用。 这样便不必在你每次要调用模式匹配方法时都实例化 xref:system.text.regularexpressions.regex 对象,还允许正则表达式引擎从其缓存中检索正则表达式的已编译版本。

using system;
using system.text.regularexpressions;

public class regexlib
{
   public static bool isvalidcurrency(string currencyvalue)
   {
      string pattern = @"\p{sc}+\s*\d+";
      return regex.ismatch(currencyvalue, pattern);
   }
}

默认情况下,将缓存最后 15 个最近使用的静态正则表达式模式。 对于需要大量已缓存的静态正则表达式的应用程序,可通过设置 regex.cachesize 属性来调整缓存大小。

此示例中使用的正则表达式 \p{sc}+\s*\d+ 可验证输入字符串是否包含一个货币符号和至少一个十进制数。 模式的定义如下表所示。

模式描述
\p{sc}+与 unicode 符号、货币类别中的一个或多个字符匹配。
\s*匹配零个或多个空白字符。
\d+匹配一个或多个十进制数字。

已解释与已编译的正则表达式

将解释未通过 regexoptions.compiled 选项的规范绑定到正则表达式引擎的正则表达式模式。 在实例化正则表达式对象时,正则表达式引擎会将正则表达式转换为一组操作代码。 调用实例方法时,操作代码会转换为 msil 并由 jit 编译器执行。 同样,当调用一种静态正则表达式方法并且在缓存中找不到该正则表达式时,正则表达式引擎会将该正则表达式转换为一组操作代码并将其存储在缓存中。 然后,它将这些操作代码转换为 msil,以便于 jit 编译器执行。 已解释的正则表达式会减少启动时间,但会使执行速度变慢。 因此,在少数方法调用中使用正则表达式时或调用正则表达式方法的确切数量未知但预期很小时,使用已解释的正则表达式的效果最佳。 随着方法调用数量的增加,执行速度变慢对性能的影响会超过减少启动时间带来的性能改进。

将编译通过 regexoptions.compiled 选项的规范绑定到正则表达式引擎的正则表达式模式。 这意味着,当实例化正则表达式对象时或当调用一种静态正则表达式方法并且在缓存中找不到该正则表达式时,正则表达式引擎会将该正则表达式转换为一组中间操作代码,这些代码之后会转换为 msil。 调用方法时,jit 编译器将执行该 msil。 与已解释的正则表达式相比,已编译的正则表达式增加了启动时间,但执行各种模式匹配方法的速度更快。 因此,相对于调用的正则表达式方法的数量,因编译正则表达式而产生的性能产生了改进。

简言之,当你使用特定正则表达式调用正则表达式方法相对不频繁时,建议使用已解释的正则表达式。 当你使用特定正则表达式调用正则表达式方法相对频繁时,应使用已编译的正则表达式。 很难确定已解释的正则表达式执行速度减慢超出启动时间减少带来的性能增益的确切阈值,或已编译的正则表达式启动速度减慢超出执行速度加快带来的性能增益的阈值。 这依赖于各种因素,包括正则表达式的复杂程度和它处理的特定数据。 若要确定已解释或已编译的正则表达式是否可为特定应用程序方案提供最佳性能,可以使用 diagnostics.stopwatch 类来比较其执行时间。

下面的示例比较了已编译和已解释正则表达式在读取 theodore dreiser 所著《金融家》中前十句文本和所有句文本时的性能。 如示例输出所示,当只对匹配方法的正则表达式进行十次调用时,已解释的正则表达式与已编译的正则表达式相比,可提供更好的性能。 但是,当进行大量调用(在此示例中,超过 13,000 次调用)时,已编译的正则表达式可提供更好的性能。

using system;
using system.diagnostics;
using system.io;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      string pattern = @"\b(\w+((\r?\n)|,?\s))*\w+[.?:;!]";
      stopwatch sw;
      match match;
      int ctr;

      streamreader infile = new streamreader(@".\dreiser_thefinancier.txt");
      string input = infile.readtoend();
      infile.close();

      // read first ten sentences with interpreted regex.
      console.writeline("10 sentences with interpreted regex:");
      sw = stopwatch.startnew();
      regex int10 = new regex(pattern, regexoptions.singleline);
      match = int10.match(input);
      for (ctr = 0; ctr <= 9; ctr++) {
         if (match.success)
            // do nothing with the match except get the next match.
            match = match.nextmatch();
         else
            break;
      }
      sw.stop();
      console.writeline("   {0} matches in {1}", ctr, sw.elapsed);

      // read first ten sentences with compiled regex.
      console.writeline("10 sentences with compiled regex:");
      sw = stopwatch.startnew();
      regex comp10 = new regex(pattern,
                   regexoptions.singleline | regexoptions.compiled);
      match = comp10.match(input);
      for (ctr = 0; ctr <= 9; ctr++) {
         if (match.success)
            // do nothing with the match except get the next match.
            match = match.nextmatch();
         else
            break;
      }
      sw.stop();
      console.writeline("   {0} matches in {1}", ctr, sw.elapsed);

      // read all sentences with interpreted regex.
      console.writeline("all sentences with interpreted regex:");
      sw = stopwatch.startnew();
      regex intall = new regex(pattern, regexoptions.singleline);
      match = intall.match(input);
      int matches = 0;
      while (match.success) {
         matches++;
         // do nothing with the match except get the next match.
         match = match.nextmatch();
      }
      sw.stop();
      console.writeline("   {0:n0} matches in {1}", matches, sw.elapsed);

      // read all sentences with compiled regex.
      console.writeline("all sentences with compiled regex:");
      sw = stopwatch.startnew();
      regex compall = new regex(pattern,
                      regexoptions.singleline | regexoptions.compiled);
      match = compall.match(input);
      matches = 0;
      while (match.success) {
         matches++;
         // do nothing with the match except get the next match.
         match = match.nextmatch();
      }
      sw.stop();
      console.writeline("   {0:n0} matches in {1}", matches, sw.elapsed);
   }
}
// the example displays the following output:
//       10 sentences with interpreted regex:
//          10 matches in 00:00:00.0047491
//       10 sentences with compiled regex:
//          10 matches in 00:00:00.0141872
//       all sentences with interpreted regex:
//          13,443 matches in 00:00:01.1929928
//       all sentences with compiled regex:
//          13,443 matches in 00:00:00.7635869
//
//       >compare1
//       10 sentences with interpreted regex:
//          10 matches in 00:00:00.0046914
//       10 sentences with compiled regex:
//          10 matches in 00:00:00.0143727
//       all sentences with interpreted regex:
//          13,443 matches in 00:00:01.1514100
//       all sentences with compiled regex:
//          13,443 matches in 00:00:00.7432921

该示例中使用的正则表达式模式 \b(\w+((\r?\n)|,?\s))*\w+[.?:;!] 的定义如下表所示。

模式描述
\b在单词边界处开始匹配。
\w+匹配一个或多个单词字符。
(\r?\n)|,?\s)匹配零个或一个回车符后跟一个换行符,或零个或一个逗号后跟一个空白字符。
(\w+((\r?\n)|,?\s))*匹配一个或多个单词字符的零个或多个事例,后跟零个或一个回车符和换行符,或后跟零个或一个逗号、一个空格字符。
\w+匹配一个或多个单词字符。
[.?:;!]匹配句号、问号、冒号、分号或感叹号。

正则表达式:编译为程序集

借助 .net,还可以创建包含已编译正则表达式的程序集。 这样会将正则表达式编译对性能造成的影响从运行时转移到设计时。 但是,这还涉及一些其他工作:必须提前定义正则表达式并将其编译为程序集。 然后,编译器在编译使用该程序集的正则表达式的源代码时,可以引用此程序集。 程序集内的每个已编译正则表达式都由从 xref:system.text.regularexpressions.regex 派生的类来表示。

若要将正则表达式编译为程序集,可调用 regex.compiletoassembly(regexcompilationinfo[], assemblyname) 方法并向其传递表示要编译的正则表达式的 regexcompilationinfo 对象数组和包含有关要创建的程序集的信息的 assemblyname 对象。

建议你在以下情况下将正则表达式编译为程序集:

  • 如果你是要创建可重用正则表达式库的组件开发人员。

  • 如果你预期正则表达式的模式匹配方法要被调用的次数无法确定 -- 从任意位置,次数可能为一次两次到上千上万次。 与已编译或已解释的正则表达式不同,编译为单独程序集的正则表达式可提供与方法调用数量无关的一致性能。

如果使用已编译的正则表达式来优化性能,则不应使用反射来创建程序集,加载正则表达式引擎并执行其模式匹配方法。 这要求你避免动态生成正则表达式模式,并且要在创建程序集时指定模式匹配选项(如不区分大小写的模式匹配)。 它还要求将创建程序集的代码与使用正则表达式的代码分离。

下面的示例演示如何创建包含已编译的正则表达式的程序集。 它创建包含一个正则表达式类 sentencepattern 的程序集 regexlib.dll,其中包含已解释与已编译的正则表达式部分中使用的句子匹配的正则表达式模式。

using system;
using system.reflection;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      regexcompilationinfo sentencepattern =
                           new regexcompilationinfo(@"\b(\w+((\r?\n)|,?\s))*\w+[.?:;!]",
                                                    regexoptions.multiline,
                                                    "sentencepattern",
                                                    "utilities.regularexpressions",
                                                    true);
      regexcompilationinfo[] regexes = { sentencepattern };
      assemblyname assemname = new assemblyname("regexlib, version=1.0.0.1001, culture=neutral, publickeytoken=null");
      regex.compiletoassembly(regexes, assemname);
   }
}

在将示例编译为可执行文件并运行时,它会创建一个名为 regexlib.dll 的程序集。 正则表达式用名为 utilities.regularexpressions.sentencepattern 并由 regularexpressions.regex 派生的类来表示。 然后,下面的示例使用已编译正则表达式,从 theodore dreiser 所著《金融家》文本中提取句子。

using system;
using system.io;
using system.text.regularexpressions;
using utilities.regularexpressions;

public class example
{
   public static void main()
   {
      sentencepattern pattern = new sentencepattern();
      streamreader infile = new streamreader(@".\dreiser_thefinancier.txt");
      string input = infile.readtoend();
      infile.close();

      matchcollection matches = pattern.matches(input);
      console.writeline("found {0:n0} sentences.", matches.count);
   }
}
// the example displays the following output:
//      found 13,443 sentences.

控制回溯

通常,正则表达式引擎使用线性进度在输入字符串中移动并将其编译为正则表达式模式。 但是,当在正则表达式模式中使用不确定限定符(如 *+ 和 ?)时,正则表达式引擎可能会放弃一部分成功的分部匹配,并返回以前保存的状态,以便为整个模式搜索成功匹配。 此过程称为回溯。

支持回溯可为正则表达式提供强大的功能和灵活性。 还可将控制正则表达式引擎操作的职责交给正则表达式开发人员来处理。 由于开发人员通常不了解此职责,因此其误用回溯或依赖过多回溯通常会显著降低正则表达式的性能。 在最糟糕的情况下,输入字符串中每增加一个字符,执行时间会加倍。 实际上,如果过多使用回溯,则在输入与正则表达式模式近似匹配时很容易创建无限循环的编程等效形式;正则表达式引擎可能需要几小时甚至几天来处理相对短的输入字符串。

通常,尽管回溯不是匹配所必需的,但应用程序会因使用回溯而对性能产生负面影响。 例如,正则表达式 \b\p{lu}\w*\b 将匹配以大写字符开头的所有单词,如下表所示。

模式描述
\b在单词边界处开始匹配。
\p{lu}匹配大写字符。
\w*匹配零个或多个单词字符。
\b在单词边界处结束匹配。

由于单词边界与单词字符不同也不是其子集,因此正则表达式引擎在匹配单词字符时无法跨越单词边界。 这意味着,对于此正则表达式而言,回溯对任何匹配的总体成功不会有任何贡献 -- 由于正则表达式引擎被强制为单词字符的每个成功的初步匹配保存其状态,因此它只会降低性能。

如果确定不需要回溯,可使用 (?>subexpression) 语言元素(被称为原子组)来禁用它。 下面的示例通过使用两个正则表达式来分析输入字符串。 第一个正则表达式 \b\p{lu}\w*\b 依赖于回溯。 第二个正则表达式 \b\p{lu}(?>\w*)\b 禁用回溯。 如示例输出所示,这两个正则表达式产生的结果相同。

using system;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      string input = "this this word sentence name capital";
      string pattern = @"\b\p{lu}\w*\b";
      foreach (match match in regex.matches(input, pattern))
         console.writeline(match.value);

      console.writeline();

      pattern = @"\b\p{lu}(?>\w*)\b";
      foreach (match match in regex.matches(input, pattern))
         console.writeline(match.value);
   }
}
// the example displays the following output:
//       this
//       sentence
//       capital
//
//       this
//       sentence
//       capital

在许多情况下,在将正则表达式模式与输入文本匹配时,回溯很重要。 但是,过度回溯会严重降低性能,并且会产生应用程序已停止响应的感觉。 特别需要指出的是,当嵌套限定符并且与外部子表达式匹配的文本为与内部子表达式匹配的文本的子集时,尤其会出现这种情况。

例如,正则表达式模式 ^[0-9a-z]([-.\w]*[0-9a-z])*\$$ 用于匹配至少包括一个字母数字字符的部件号。 任何附加字符可以包含字母数字字符、连字符、下划线或句号,但最后一个字符必须为字母数字。 美元符号用于终止部件号。 在某些情况下,由于限定符嵌套并且子表达式 [0-9a-z] 是子表达式 [-.\w]* 的子集,因此此正则表达式模式会表现出极差的性能。

在这些情况下,可通过移除嵌套限定符并将外部子表达式替换为零宽度预测先行和回顾断言来优化正则表达式性能。 预测先行和回顾断言是定位点;它们不在输入字符串中移动指针,而是通过预测先行或回顾来检查是否满足指定条件。 例如,可将部件号正则表达式重写为 ^[0-9a-z][-.\w]*(?<=[0-9a-z])\$$。 此正则表达式模式的定义如下表所示。

模式描述
^从输入字符串的开头部分开始匹配。
[0-9a-z]匹配字母数字字符。 部件号至少要包含此字符。
[-.\w]*匹配零个或多个任意单词字符、连字符或句号。
\$匹配美元符号。
(?<=[0-9a-z])查看作为结束的美元符号,以确保前一个字符是字母数字。
$在输入字符串末尾结束匹配。

下面的示例演示了如何使用此正则表达式来匹配包含可能部件号的数组。

using system;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      string pattern = @"^[0-9a-z][-.\w]*(?<=[0-9a-z])\$$";
      string[] partnos = { "a1c$", "a4", "a4$", "a1603d$", "a1603d#" };

      foreach (var input in partnos) {
         match match = regex.match(input, pattern);
         if (match.success)
            console.writeline(match.value);
         else
            console.writeline("match not found.");
      }
   }
}
// the example displays the following output:
//       a1c$
//       match not found.
//       a4$
//       a1603d$
//       match not found.

.net 中的正则表达式语言包括以下可用于消除嵌套限定符的语言元素。 有关详细信息,请参阅分组构造

语言元素描述
(?= subexpression )零宽度正预测先行。 预测先行当前位置,以确定 subexpression 是否与输入字符串匹配。
(?! subexpression )零宽度负预测先行。 预测先行当前位置,以确定 subexpression 是否不与输入字符串匹配。
(?<= subexpression )零宽度正回顾。 回顾后发当前位置,以确定 subexpression 是否与输入字符串匹配。
(?<! subexpression )零宽度负回顾。 回顾后发当前位置,以确定 subexpression 是否不与输入字符串匹配。

使用超时值

如果正则表达式处理与正则表达式模式大致匹配的输入,则通常依赖于会严重影响其性能的过度回溯。 除认真考虑对回溯的使用以及针对大致匹配输入对正则表达式进行测试之外,还应始终设置一个超时值以确保最大程度地降低过度回溯的影响(如果有)。

正则表达式超时间隔定义了在超时前正则表达式引擎用于查找单个匹配项的时间长度。默认超时间隔为 regex.infinitematchtimeout,这意味着正则表达式不会超时。可以按如下所示重写此值并定义超时间隔:

  • 在实例化一个 regex 对象(通过调用 regex(string, regexoptions, timespan) 构造函数)时,提供一个超时值。
  • 调用静态模式匹配方法,如 regex.match(string, string, regexoptions, timespan) 或 regex.replace(string, string, string, regexoptions, timespan),其中包含 matchtimeout 参数。
  • 对于通过调用 regex.compiletoassembly 方法创建的已编译的正则表达式,可调用带有 timespan 类型的参数的构造函数。

如果定义了超时间隔并且在此间隔结束时未找到匹配项,则正则表达式方法将引发 regexmatchtimeoutexception 异常。 在异常处理程序中,可以选择使用一个更长的超时间隔来重试匹配、放弃匹配尝试并假定没有匹配项,或者放弃匹配尝试并记录异常信息以供未来分析。

下面的示例定义了一种 getworddata 方法,此方法实例化了一个正则表达式,使其具有 350 毫秒的超时间隔,用于计算文本文件中的词语数和一个词语中的平均字符数。 如果匹配操作超时,则超时间隔将延长 350 毫秒并重新实例化 regex 对象。 如果新的超时间隔超过 1 秒,则此方法将再次向调用方引发异常。

using system;
using system.collections.generic;
using system.io;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      regexutilities util = new regexutilities();
      string title = "doyle - the hound of the baskervilles.txt";
      try {
         var info = util.getworddata(title);
         console.writeline("words:               {0:n0}", info.item1);
         console.writeline("average word length: {0:n2} characters", info.item2);
      }
      catch (ioexception e) {
         console.writeline("ioexception reading file '{0}'", title);
         console.writeline(e.message);
      }
      catch (regexmatchtimeoutexception e) {
         console.writeline("the operation timed out after {0:n0} milliseconds",
                           e.matchtimeout.totalmilliseconds);
      }
   }
}

public class regexutilities
{
   public tuple<int, double> getworddata(string filename)
   {
      const int max_timeout = 1000;   // maximum timeout interval in milliseconds.
      const int increment = 350;      // milliseconds increment of timeout.

      list<string> exclusions = new list<string>( new string[] { "a", "an", "the" });
      int[] wordlengths = new int[29];        // allocate an array of more than ample size.
      string input = null;
      streamreader sr = null;
      try {
         sr = new streamreader(filename);
         input = sr.readtoend();
      }
      catch (filenotfoundexception e) {
         string msg = string.format("unable to find the file '{0}'", filename);
         throw new ioexception(msg, e);
      }
      catch (ioexception e) {
         throw new ioexception(e.message, e);
      }
      finally {
         if (sr != null) sr.close();
      }

      int timeoutinterval = increment;
      bool init = false;
      regex rgx = null;
      match m = null;
      int indexpos = 0;
      do {
         try {
            if (! init) {
               rgx = new regex(@"\b\w+\b", regexoptions.none,
                               timespan.frommilliseconds(timeoutinterval));
               m = rgx.match(input, indexpos);
               init = true;
            }
            else {
               m = m.nextmatch();
            }
            if (m.success) {
               if ( !exclusions.contains(m.value.tolower()))
                  wordlengths[m.value.length]++;

               indexpos += m.length + 1;
            }
         }
         catch (regexmatchtimeoutexception e) {
            if (e.matchtimeout.totalmilliseconds < max_timeout) {
               timeoutinterval += increment;
               init = false;
            }
            else {
               // rethrow the exception.
               throw;
            }
         }
      } while (m.success);

      // if regex completed successfully, calculate number of words and average length.
      int nwords = 0;
      long totallength = 0;

      for (int ctr = wordlengths.getlowerbound(0); ctr <= wordlengths.getupperbound(0); ctr++) {
         nwords += wordlengths[ctr];
         totallength += ctr * wordlengths[ctr];
      }
      return new tuple<int, double>(nwords, totallength/nwords);
   }
}

只在必要时捕获

.net 中的正则表达式支持许多分组构造,这样,便可以将正则表达式模式分组为一个或多个子表达式。 .net 正则表达式语言中最常用的分组构造为 (subexpression)(用于定义编号捕获组)和 (?subexpression)(用于定义命名捕获组)。 分组构造是创建反向引用和定义要应用限定符的子表达式时所必需的。

但是,使用这些语言元素会产生一定的开销。 它们会导致用最近的未命名或已命名捕获来填充 groupcollection 属性返回的 match.groups 对象,如果单个分组构造已捕获输入字符串中的多个子字符串,则还会填充包含多个 capturecollection 对象的特定捕获组的 group.captures 属性返回的 capture 对象。

通常,只在正则表达式中使用分组构造,这样可对其应用限定符,而且以后不会使用这些子表达式捕获的组。 例如,正则表达式 \b(\w+[;,]?\s?)+[.?!] 用于捕获整个句子。 下表描述了此正则表达式模式中的语言元素及其对 match 对象的 match.groups 和 group.captures 集合的影响。

模式描述
\b在单词边界处开始匹配。
\w+匹配一个或多个单词字符。
[;,]?匹配零个或一个逗号或分号。
\s?匹配零个或一个空白字符。
(\w+[;,]?\s?)+匹配以下一个或多个事例:一个或多个单词字符,后跟一个可选逗号或分号,一个可选的空白字符。 用于定义第一个捕获组,它是必需的,以便将重复多个单词字符的组合(即单词)后跟可选标点符号,直至正则表达式引擎到达句子末尾。
[.?!]匹配句号、问号或感叹号。

如下面的示例所示,当找到匹配时,groupcollection 和 capturecollection 对象都将用匹配中的捕获内容来填充。 在此情况下,存在捕获组 (\w+[;,]?\s?),因此可对其应用 + 限定符,从而使得正则表达式模式可与句子中的每个单词匹配。 否则,它将匹配句子中的最后一个单词。

using system;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      string input = "this is one sentence. this is another.";
      string pattern = @"\b(\w+[;,]?\s?)+[.?!]";

      foreach (match match in regex.matches(input, pattern)) {
         console.writeline("match: '{0}' at index {1}.",
                           match.value, match.index);
         int grpctr = 0;
         foreach (group grp in match.groups) {
            console.writeline("   group {0}: '{1}' at index {2}.",
                              grpctr, grp.value, grp.index);
            int capctr = 0;
            foreach (capture cap in grp.captures) {
               console.writeline("      capture {0}: '{1}' at {2}.",
                                 capctr, cap.value, cap.index);
               capctr++;
            }
            grpctr++;
         }
         console.writeline();
      }
   }
}
// the example displays the following output:
//       match: 'this is one sentence.' at index 0.
//          group 0: 'this is one sentence.' at index 0.
//             capture 0: 'this is one sentence.' at 0.
//          group 1: 'sentence' at index 12.
//             capture 0: 'this ' at 0.
//             capture 1: 'is ' at 5.
//             capture 2: 'one ' at 8.
//             capture 3: 'sentence' at 12.
//
//       match: 'this is another.' at index 22.
//          group 0: 'this is another.' at index 22.
//             capture 0: 'this is another.' at 22.
//          group 1: 'another' at index 30.
//             capture 0: 'this ' at 22.
//             capture 1: 'is ' at 27.
//             capture 2: 'another' at 30.

当你只使用子表达式来对其应用限定符并且你对捕获的文本不感兴趣时,应禁用组捕获。 例如,(?:subexpression) 语言元素可防止应用此元素的组捕获匹配的子字符串。 在下面的示例中,上一示例中的正则表达式模式更改为 \b(?:\w+[;,]?\s?)+[.?!]。 正如输出所示,它禁止正则表达式引擎填充 groupcollection 和 capturecollection 集合。

using system;
using system.text.regularexpressions;

public class example
{
   public static void main()
   {
      string input = "this is one sentence. this is another.";
      string pattern = @"\b(?:\w+[;,]?\s?)+[.?!]";

      foreach (match match in regex.matches(input, pattern)) {
         console.writeline("match: '{0}' at index {1}.",
                           match.value, match.index);
         int grpctr = 0;
         foreach (group grp in match.groups) {
            console.writeline("   group {0}: '{1}' at index {2}.",
                              grpctr, grp.value, grp.index);
            int capctr = 0;
            foreach (capture cap in grp.captures) {
               console.writeline("      capture {0}: '{1}' at {2}.",
                                 capctr, cap.value, cap.index);
               capctr++;
            }
            grpctr++;
         }
         console.writeline();
      }
   }
}
// the example displays the following output:
//       match: 'this is one sentence.' at index 0.
//          group 0: 'this is one sentence.' at index 0.
//             capture 0: 'this is one sentence.' at 0.
//
//       match: 'this is another.' at index 22.
//          group 0: 'this is another.' at index 22.
//             capture 0: 'this is another.' at 22.

可以通过以下方式之一来禁用捕获:

  • 使用 (?:subexpression) 语言元素。 此元素可防止在它应用的组中捕获匹配的子字符串。 它不在任何嵌套的组中禁用子字符串捕获。
  • 使用 explicitcapture 选项。 在正则表达式模式中禁用所有未命名或隐式捕获。 使用此选项时,只能捕获与使用 (?subexpression) 语言元素定义的命名组匹配的子字符串。 可将 explicitcapture 标记传递给 options 类构造函数的 regex 参数或 options 静态匹配方法的 regex 参数。
  • 在 n 语言元素中使用 (?imnsx) 选项。 此选项将在元素出现的正则表达式模式中的点处禁用所有未命名或隐式捕获。 捕获将一直禁用到模式结束或 (-n) 选项启用未命名或隐式捕获。 有关详细信息,请参阅 其他构造。
  • 在 n 语言元素中使用 (?imnsx:subexpression) 选项。 此选项可在 subexpression 中禁用所有未命名或隐式捕获。 同时禁用任何未命名或隐式的嵌套捕获组进行的任何捕获。

原文链接

到此这篇关于.net正则表达式最佳用法的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com