Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default regular expression parse string Perl $1 VBScript.RegExp

I want to use a VBScript regular expression to parse a string in VBA.

Can some kind soul get this to work?

Thanks.

Sub x()

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
Set oMatch = Matches.subMatches(0)

' I can enumerate through Matches but the enumeration doesn't include
"Joe"

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default regular expression parse string Perl $1 VBScript.RegExp

On Fri, 13 Jul 2007 17:50:34 -0700, "
wrote:

I want to use a VBScript regular expression to parse a string in VBA.

Can some kind soul get this to work?

Thanks.

Sub x()

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
Set oMatch = Matches.subMatches(0)

' I can enumerate through Matches but the enumeration doesn't include
"Joe"

End Sub



Sub x()
Dim regEx As Object
Dim Matches As Object

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
' Set oMatch = Matches.subMatches(0)

Debug.Print Matches(0).submatches(0)

End Sub


--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default regular expression parse string Perl $1 VBScript.RegExp

That works. Thanks Ron!

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default regular expression parse string Perl $1 VBScript.RegExp

Ron,

How would you use it to find multiple matches: if

s = "First Name: Joe, First Name: Ron"

your debug.print returns Joe - how do you get to Ron as well?

TIA,
Bernie


"Ron Rosenfeld" wrote in message
...
On Fri, 13 Jul 2007 17:50:34 -0700, "
wrote:

I want to use a VBScript regular expression to parse a string in VBA.

Can some kind soul get this to work?

Thanks.

Sub x()

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
Set oMatch = Matches.subMatches(0)

' I can enumerate through Matches but the enumeration doesn't include
"Joe"

End Sub



Sub x()
Dim regEx As Object
Dim Matches As Object

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
' Set oMatch = Matches.subMatches(0)

Debug.Print Matches(0).submatches(0)

End Sub


--ron



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default regular expression parse string Perl $1 VBScript.RegExp

How would you use it to find multiple matches: if
s = "First Name: Joe, First Name: Ron"


Hi Bernie. Try setting the Global search to True.

Sub x()
Dim regEx As Object
Dim Matches As Object
Dim s As String

s = "First Name: Joe, First Name: Ron"

Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
regEx.Global = True
regEx.IgnoreCase = True

Set Matches = regEx.Execute(s)

Debug.Print Matches(0).Submatches(0)
Debug.Print Matches(1).Submatches(0)

'or
Dim sName
For Each sName In Matches
Debug.Print sName.Submatches(0)
Next sName
End Sub

--
Dana DeLouis


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Ron,

How would you use it to find multiple matches: if

s = "First Name: Joe, First Name: Ron"

your debug.print returns Joe - how do you get to Ron as well?

TIA,
Bernie


"Ron Rosenfeld" wrote in message
...
On Fri, 13 Jul 2007 17:50:34 -0700, "
wrote:

I want to use a VBScript regular expression to parse a string in VBA.

Can some kind soul get this to work?

Thanks.

Sub x()

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
Set oMatch = Matches.subMatches(0)

' I can enumerate through Matches but the enumeration doesn't include
"Joe"

End Sub



Sub x()
Dim regEx As Object
Dim Matches As Object

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
' Set oMatch = Matches.subMatches(0)

Debug.Print Matches(0).submatches(0)

End Sub


--ron







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default regular expression parse string Perl $1 VBScript.RegExp

On Fri, 13 Jul 2007 22:40:20 -0400, "Bernie Deitrick" <deitbe @ consumer dot
org wrote:

Ron,

How would you use it to find multiple matches: if

s = "First Name: Joe, First Name: Ron"

your debug.print returns Joe - how do you get to Ron as well?

TIA,
Bernie


Bernie,

Excerpted From:
http://support.microsoft.com/default...02&Product=vbb

• Global: Sets a Boolean value or returns a Boolean value that indicates
whether a pattern must match all the occurrences in a whole search string, or
whether a pattern must match just the first occurrence.

In my response to the OP, I was just modifying his routine. But, ordinarily I
set the Global property to True, so as to enable multiple matches.

Here's the routine modified, with the changes noted:

==========================================
Option Explicit

Sub x()
Dim regEx As Object
Dim Matches As Object
'-------------------
Dim i As Long
'--------------------
Dim s As String
s = "First Name: Joe First Name: Bernie"
Set regEx = CreateObject("VBScript.RegExp")
'-----------------------
regEx.Global = True
'-----------------------
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

'------------------------------
For i = 0 To Matches.Count - 1
Debug.Print Matches(i).submatches(0)
Next i
'--------------------------------

End Sub
==============================================
--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default regular expression parse string Perl $1 VBScript.RegExp

On Fri, 13 Jul 2007 22:40:20 -0400, "Bernie Deitrick" <deitbe @ consumer dot
org wrote:

Ron,

How would you use it to find multiple matches: if

s = "First Name: Joe, First Name: Ron"

your debug.print returns Joe - how do you get to Ron as well?

TIA,
Bernie


Bernie,

Excerpted From:
http://support.microsoft.com/default...02&Product=vbb

• Global: Sets a Boolean value or returns a Boolean value that indicates
whether a pattern must match all the occurrences in a whole search string, or
whether a pattern must match just the first occurrence.

In my response to the OP, I was just modifying his routine. But, ordinarily I
set the Global property to True, so as to enable multiple matches.

Here's the routine modified, with the changes noted:

==========================================
Option Explicit

Sub x()
Dim regEx As Object
Dim Matches As Object
'-------------------
Dim i As Long
'--------------------
Dim s As String
s = "First Name: Joe First Name: Bernie"
Set regEx = CreateObject("VBScript.RegExp")
'-----------------------
regEx.Global = True
'-----------------------
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

'------------------------------
For i = 0 To Matches.Count - 1
Debug.Print Matches(i).submatches(0)
Next i
'--------------------------------

End Sub
==============================================
--ron
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default regular expression parse string Perl $1 VBScript.RegExp

Should there be no Names in the string, one test to use is:
If Matches.Count 0 Then ...

Other articles say it's better to use "Test."
I'm not sure which is faster/better in this case.

Sub Demo()
Dim Matches As Object
Dim s As String
Dim sName

s = "First Name: Joe, First Name: Ron"
'or
s = "Text withe no Names"

With CreateObject("VBScript.RegExp")
.Pattern = "First Name: (\w+)"
.Global = True
.ignorecase = True

If .Test(s) Then
Set Matches = .Execute(s)
For Each sName In Matches
Debug.Print sName.Submatches(0)
Next sName
Else
'No Names
End If
End With
End Sub

--
Dana DeLouis


"Dana DeLouis" wrote in message
...
How would you use it to find multiple matches: if
s = "First Name: Joe, First Name: Ron"


Hi Bernie. Try setting the Global search to True.

Sub x()
Dim regEx As Object
Dim Matches As Object
Dim s As String

s = "First Name: Joe, First Name: Ron"

Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
regEx.Global = True
regEx.IgnoreCase = True

Set Matches = regEx.Execute(s)

Debug.Print Matches(0).Submatches(0)
Debug.Print Matches(1).Submatches(0)

'or
Dim sName
For Each sName In Matches
Debug.Print sName.Submatches(0)
Next sName
End Sub

--
Dana DeLouis


"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Ron,

How would you use it to find multiple matches: if

s = "First Name: Joe, First Name: Ron"

your debug.print returns Joe - how do you get to Ron as well?

TIA,
Bernie


"Ron Rosenfeld" wrote in message
...
On Fri, 13 Jul 2007 17:50:34 -0700, "
wrote:

I want to use a VBScript regular expression to parse a string in VBA.

Can some kind soul get this to work?

Thanks.

Sub x()

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
Set oMatch = Matches.subMatches(0)

' I can enumerate through Matches but the enumeration doesn't include
"Joe"

End Sub


Sub x()
Dim regEx As Object
Dim Matches As Object

Dim s As String
s = "First Name: Joe"
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "First Name: (\w+)"
Set Matches = regEx.Execute(s)

' This statement fails - no such property
' Set oMatch = Matches.subMatches(0)

Debug.Print Matches(0).submatches(0)

End Sub


--ron







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone help me with this regular expression? [email protected] Excel Discussion (Misc queries) 3 March 10th 09 07:36 PM
Help with regular expression PO Excel Programming 3 May 2nd 07 01:39 PM
Regular Expression sl Excel Discussion (Misc queries) 2 January 23rd 07 11:57 PM
Declare RegExp from Vbscript.dll Bob Phillips Excel Programming 0 January 16th 07 07:45 PM
Regular Expression for cell address M. Authement Excel Programming 11 January 4th 07 08:57 PM


All times are GMT +1. The time now is 01:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"