471,605 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

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.RegularExpressions
'..
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.IgnoreCase)
If r.Match(s).Success Then
Console.Write(r.Match(s).Result("$1"))


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

if ( r.Match(s).Success)
{
Console.Write(r.Match(s).Result("$1"));
}

Jan 29 '06 #1
2 1516
> 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.RegularExpressions
'..
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.IgnoreCase)
If r.Match(s).Success Then
Console.Write(r.Match(s).Result("$1"))


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

if ( r.Match(s).Success)
{
Console.Write(r.Match(s).Result("$1"));
}


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

Hans Kesting
Jan 30 '06 #2
an******@hotmail.com wrote:
// C#
using System.Text.RegularExpressions;
//...
String s = "Grennwich time 12:15pm September 1, 2001";
Regex r = new Regex(".*(\d{2}:\d{2}[ap]m)", RegexOptions.IgnoreCase);

if ( r.Match(s).Success)
{
Console.Write(r.Match(s).Result("$1"));
}


I find your use of r.Match(s).Result("$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(inputstring, "\s+(\d{2}:\d{2}[ap]m)",
RegexOptions.IgnoreCase).Group(1).Value;

vs

Regex.Match(inputstring, "\s+(\d{2}:\d{2}[ap]m)",
RegexOptions.IgnoreCase).Result("$1");

--
Jay R. Wren
Jan 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.