Regular expressions

J

JeffMelton

I've been working with some html and am trying to use regular
expressions to get a value. Here is what I'm looking for
"handStatus[0] = '(I need this data)';"

I need to find the part that says I need this data, the () are not part
of the string but the ' are. This is what I've tried, but I've never
really worked with Regexp, could somebody tell me what I need to fix?

Set RegEx = New RegExp
anyStr = myIE.document.body.innerText
RegEx.Pattern = "handStatus[0] = '(\w)';"
RegEx.IgnoreCase = True
RegEx.Global = True ' Set global applicability.
Set match = RegEx.Execute(anyStr) ' Execute search.
MsgBox (match.Value)

Thanks
 
D

Dana DeLouis

Here's one way. There may be a better way to test for a character other
than \D\d, but I don't know it off hand. Maybe someone can jump in with a
better test.

Sub TestIt()
Debug.Print GetString("handStatus[0] = 'I need this data' ")
End Sub

Function GetString(s As String)
Dim re
Set re = New RegExp

re.IgnoreCase = True
re.Global = True
re.MultiLine = True
re.Pattern = "'([\D|\d]+)'"

If re.test(s) Then
GetString = re.Execute(s)(0).SubMatches(0)
End If
End Function
 
P

PaulD

: Here's one way. There may be a better way to test for a character other
: than \D\d, but I don't know it off hand. Maybe someone can jump in with a
: better test.
:
: Sub TestIt()
: Debug.Print GetString("handStatus[0] = 'I need this data' ")
: End Sub
:
: Function GetString(s As String)
: Dim re
: Set re = New RegExp
:
: re.IgnoreCase = True
: re.Global = True
: re.MultiLine = True
: re.Pattern = "'([\D|\d]+)'"
:
: If re.test(s) Then
: GetString = re.Execute(s)(0).SubMatches(0)
: End If
: End Function
:
: --
: HTH. :>)
: Dana DeLouis
: Windows XP, Office 2003

Considering this is HTML, what if there is an entry that has "FootStatus[0]
= ... or "handStatus[1]=...
To only search for what was stated try this

Public Sub NeedData()
Dim anyStr As String
Dim regx As RegExp
Dim match As MatchCollection

Set regx = New RegExp
anyStr = myIE.document.body.innerText
regx.Pattern = "(handStatus\[0\] = ')([\w|\s]+)'"
regx.IgnoreCase = True
regx.Global = True ' Set global applicability.
Set match = regx.Execute(anyStr) ' Execute search.
If match.Count > 0 Then
MsgBox match(0).SubMatches(1)
End If
Set regx = Nothing
Set match = Nothing
End Sub

This routine only returns the first match of this string 'match(0)', if it
is possible to have more than one match, you should iterate through the
matchcollection to get all the values.
Paul D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top