How to use Regular Expression in .NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonieko@hotmail.com

    How to use Regular Expression in .NET

    Here is an example. For a time server which returns a time field
    string, you can extract the time by the following code:




    'VB.NET
    Imports System.Text.Reg ularExpressions
    '..
    Dim s As String
    Dim r As Regex
    s = "Grennwich time 12:15pm September 1, 2001"
    r = New Regex(".*(\d{2} :\d{2}[ap]m)", RegexOptions.Ig noreCase)
    If r.Match(s).Succ ess Then
    Console.Write(r .Match(s).Resul t("$1"))




    // C#
    using System.Text.Reg ularExpressions ;
    //...
    String s = "Grennwich time 12:15pm September 1, 2001";
    Regex r = new Regex(".*(\d{2} :\d{2}[ap]m)", RegexOptions.Ig noreCase);

    if ( r.Match(s).Succ ess)
    {
    Console.Write(r .Match(s).Resul t("$1"));
    }

  • Hans Kesting

    #2
    Re: How to use Regular Expression in .NET

    > Here is an example. For a time server which returns a time field[color=blue]
    > string, you can extract the time by the following code:
    >
    >
    >
    >
    > 'VB.NET
    > Imports System.Text.Reg ularExpressions
    > '..
    > Dim s As String
    > Dim r As Regex
    > s = "Grennwich time 12:15pm September 1, 2001"
    > r = New Regex(".*(\d{2} :\d{2}[ap]m)", RegexOptions.Ig noreCase)
    > If r.Match(s).Succ ess Then
    > Console.Write(r .Match(s).Resul t("$1"))
    >
    >
    >
    >
    > // C#
    > using System.Text.Reg ularExpressions ;
    > //...
    > String s = "Grennwich time 12:15pm September 1, 2001";
    > Regex r = new Regex(".*(\d{2} :\d{2}[ap]m)", RegexOptions.Ig noreCase);
    >
    > if ( r.Match(s).Succ ess)
    > {
    > Console.Write(r .Match(s).Resul t("$1"));
    > }[/color]

    Within C# strings a \ starts an "escape sequence". Use
    new Regex(@".*(\d{2 }:\d{2}[ap]m)", ...

    Hans Kesting


    Comment

    • Jay R. Wren

      #3
      Re: How to use Regular Expression in .NET

      anonieko@hotmai l.com wrote:[color=blue]
      > // C#
      > using System.Text.Reg ularExpressions ;
      > //...
      > String s = "Grennwich time 12:15pm September 1, 2001";
      > Regex r = new Regex(".*(\d{2} :\d{2}[ap]m)", RegexOptions.Ig noreCase);
      >
      > if ( r.Match(s).Succ ess)
      > {
      > Console.Write(r .Match(s).Resul t("$1"));
      > }
      >[/color]

      I find your use of r.Match(s).Resu lt("$1") interesting. I didn't know
      that one could use Match that way. Indeed I am not familiar with the
      Result() member.

      I typically use the Group() member and its Value property for extracting
      data from the Match.

      Does anyone know which is more efficient?

      Regex.Match(inp utstring, "\s+(\d{2}:\d{2 }[ap]m)",
      RegexOptions.Ig noreCase).Group (1).Value;

      vs

      Regex.Match(inp utstring, "\s+(\d{2}:\d{2 }[ap]m)",
      RegexOptions.Ig noreCase).Resul t("$1");

      --
      Jay R. Wren

      Comment

      Working...