Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expressions

Sehboo
Guest
 
Posts: n/a
#1: Nov 20 '05
Hi,

I have several regular expressions that I need to run against
documents. Is it possible to combine several expressions in one
expression in Regex object. So that it is faster, or will I have to
use all the expressions seperately?

Here are my regular expressions that check for valid email address and
link
Dim Expression As String =
"([\w\-]+\.)*[\w\-]+@([\w\-]+\.)+([\w\-]{2,5})"
EmailRegex = New Regex(Expression, RegexOptions.IgnoreCase Or
RegexOptions.Compiled)

Dim HrefPattern As String =
"href\s*=\s*(?:""(?<match>[^""]*)""|(?<match>\S+))"
HrefRegex = New Regex(HrefPattern, RegexOptions.IgnoreCase Or
RegexOptions.Compiled)

so can I combine both regular expressions in one?

Thanks

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Regular Expressions


Sehboo,
Using Alternation you can combine regular expressions.

Dim regex As New Regex(Expression & "|" & HrefPattern,
RegexOptions.IgnoreCase Or[color=blue]
> RegexOptions.Compiled)[/color]

You may want to also use a grouping construct with a name so you know which
expression you matched...

Also you may want to use Match & NextMatch to walk the matches found,
however I do not have a good example of this...


I find both of the following sites invaluable when working with regular
expressions.

A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...geElements.asp

Hope this helps
Jay



"Sehboo" <masoodadnan@hotmail.com> wrote in message
news:7bfc97ca.0404021524.6cea990d@posting.google.c om...[color=blue]
> Hi,
>
> I have several regular expressions that I need to run against
> documents. Is it possible to combine several expressions in one
> expression in Regex object. So that it is faster, or will I have to
> use all the expressions seperately?
>
> Here are my regular expressions that check for valid email address and
> link
> Dim Expression As String =
> "([\w\-]+\.)*[\w\-]+@([\w\-]+\.)+([\w\-]{2,5})"
> EmailRegex = New Regex(Expression, RegexOptions.IgnoreCase Or
> RegexOptions.Compiled)
>
> Dim HrefPattern As String =
> "href\s*=\s*(?:""(?<match>[^""]*)""|(?<match>\S+))"
> HrefRegex = New Regex(HrefPattern, RegexOptions.IgnoreCase Or
> RegexOptions.Compiled)
>
> so can I combine both regular expressions in one?
>
> Thanks[/color]


Closed Thread