Drivium
Drivium

Reputation: 537

RegExp numbers between single quotes

I have a string like this "f_details('277095');">. I just need to get the 277095 part. I've been trying variations of strPattern = "'[0-9]'+", but this is either finding nothing or finding the wrong things.

I don't understand regular expressions despite having a cheat sheet right in front of me. Spent an hour trying different things already. What would this regexp look like?

Here is my code that I use to scrape this site and grab data:

Set objWshShell = Wscript.CreateObject("Wscript.Shell")
Set IE = CreateObject("internetexplorer.application")
Set fso = CreateObject("Scripting.FileSystemObject")

on error resume next
For i=1 To 77 '77 Counties

If i=77 Then Exit For

IE.Visible = True
IE.Navigate "https://lic.ok.gov/PublicPortal/OREC/FindAssociateEntity.jsp"
Do Until IE.ReadyState = 4: WScript.sleep 15: Loop

Do Until IE.Document.ReadyState = "complete": WScript.sleep 10: Loop
IE.Document.getElementsByTagName("select")("AddrCountyCode").Value = i

Do Until IE.Document.ReadyState = "complete": WScript.sleep 10: Loop
For Each btn In IE.Document.getElementsByTagName("input")
If btn.name = "btnSearch" Then btn.Click()
NEXT

strPattern = "'(\d+)'"

strTestString = ie.document.body.innerhtml

arrAllMatches = fGetMatches(strPattern, strTestString)

If UBound(arrAllMatches) <> 0 Then 

filename = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\License.txt"

set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile(filename,8,true) 
ts.write Join(arrAllMatches, vbCrlf)
ts.close 

Else

WScript.Echo "-- None Found --"

End if

next
Wscript.echo "DONE!"

'=====================================================================
Function fGetMatches(sPattern, sStr)
Dim regEx, retVal, sMatch, colMatches, temp
Set regEx = New RegExp     ' Create a regular expression.
regEx.Pattern = sPattern   ' Set pattern.
regEx.IgnoreCase = True   ' Set case insensitivity.
regEx.Global = True        ' Set global applicability.

Set colMatches = regEx.Execute(sStr)   ' Execute search.

If colMatches.Count = 0 Then
    temp = Array("")
Else
    '# Convert Collection to Array
    For Each sMatch In colMatches
        temp = temp & sMatch & "¶"
    Next
    temp = Left(temp, Len(temp) - 1)
    temp = Split(temp, "¶")
End If

fGetMatches = temp
End Function

Upvotes: 1

Views: 3459

Answers (3)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

VBScript's regexp implementation is restricted, but if you follow the general rule "Keep it simple", even here you can cut a sequence of numbers easily:

>> Set r = New RegExp
>> r.Pattern = "\d+"
>> s = "f_details('277095');"
>> WScript.Echo r.Execute(s)(0).Value
>>
277095

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 31045

Additionaly to Vks answer, you can use capturing groups to capture the content you need.

You can use a regex like this:

'(\d+)'

Working demo

You can see highlighted in blue the match and in green the captured content

enter image description here

Match information

MATCH 1
1.  [11-17] `277095`

Upvotes: 0

vks
vks

Reputation: 67988

'\d+'

Just add quantifier to \d instead of ' as you want \d to repeat.

Try (?<=')\d+(?=') if you want to get only 277095

See demo.

https://regex101.com/r/iS6jF6/6

Dim strRegex as String = "'\d+'"
Dim myRegex As New Regex(strRegex, RegexOptions.Multiline)
Dim strTargetString As String = "f_details('277095');"

For Each myMatch As Match In myRegex.Matches(strTargetString)
     If myMatch.Success Then
     ' Add your code here
  End If
Next

Upvotes: 5

Related Questions