Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

comparing string using regular expression in VBA

14 views
Skip to first unread message

perlperl

unread,
Mar 19, 2006, 10:06:26 AM3/19/06
to
how to regular expression in VBA

my string is "The operating system is Win32"

This is what i do in perl
if $string =~ /Thes operating system is Win32|Linux|Unix/ {

}

which mean the OS can be unix Linux or Win32

How this can be achieved in VBA

John Nurick

unread,
Mar 19, 2006, 12:05:18 PM3/19/06
to
You have to use the VBScript Regexp object:

Dim oRE As Object 'VBScript_RegExp_55.RegExp

Set oRE = CreateObject("VBScript.RegExp")

oRE.Pattern = "The operating system is Win32|Linux|Unix"

If oRE.Test("The operating system is Win32") Then
'Match found

End If

For some links to documentation see
http://www.j.nurick.dial.pipex.com/Code/vbRegex/MoreAboutRegexp.htm

"perlperl" <shahr...@gmail.com> wrote in message
news:1142780786.8...@e56g2000cwe.googlegroups.com...

perlperl

unread,
Mar 20, 2006, 9:38:08 AM3/20/06
to
If oRE.Test("The operating system is Win32") Then
this won't work, as i said in my example, it can be eithe Win32, Linux
or Unix (also I don't want 3 different if statements....)

can I do

If oRE.Test("The operating system is Win32|Linux|Unix") Then

TC

unread,
Mar 20, 2006, 9:49:01 AM3/20/06
to
John showed you a pattern that will match the statement:

"The operating system is X"

where X can be "Win32", or "Linux", or "Unix".

If that's not what you want, please describe what you /do/ want.

HTH,
TC (MVP Access)
http://tc2.atspace.com

perlperl

unread,
Mar 20, 2006, 9:58:51 AM3/20/06
to
actually my bad, i din't gave the correct example.
to be more specific, i m looking to comapre IP address which can be of
the format
22.223.22.234 which is integer.integer.integer.integer (integer can be
1 or 2 or 3 digit)

i tried the pattern
oRE.Pattern = "\d+.\d+.\d+.\d+"

If oRE.Test(.Text) Then ' .Text contains the IP address

It didn't worked

perlperl

unread,
Mar 20, 2006, 10:08:16 AM3/20/06
to
If my IP is 4.4.4.4
then oRE.Pattern = "[0-9]+.[0-9]+.[0-9]+.[0-9]+" works

but if the IP is 44.4.4.4, it does not work

TC

unread,
Mar 20, 2006, 10:11:45 AM3/20/06
to
That looks ok from what I remember.

Do you need to enbracket the periods? [0-9]+[.][0-9]+[.] etc

perlperl

unread,
Mar 20, 2006, 10:16:54 AM3/20/06
to
Yep, You are absolutely correct...
I did [0-9]+\.[0-9]+\.[0-9]+\.[0-9] It worked

TC

unread,
Mar 20, 2006, 8:31:45 PM3/20/06
to
Great, well done!
0 new messages