how can i find the first instance of a number after a specific string and ignore all other characters?

Your regex does not match the strings you showed. You can use

\b5 Months[\s\S]*?(\d+(?:\.\d+)?)

See the regex demo. Details:

  • \b – a word boundary
  • 5 Months – a literal text
  • [\s\S]*? – any 0 or more chars, as few as possible
  • (\d+(?:\.\d+)?) – Capturing group 1: one or more digits followed with an optional sequence of a . and one or more digits.

Test run in VBA:

Sub TestFn()
Dim reg4 As Object: Set reg4 = CreateObject("vbscript.regexp")
 reg4.Pattern = "\b5 Months[\s\S]*?(\d+(?:\.\d+)?)"
Dim myText As String
 myText = "5 Months" & vbCrLf & vbCrLf & "0.00"
Dim MCS As Object
Set MCS = reg4.Execute(myText)
Dim Months5 As String: Months5 = MCS(0).SubMatches(0)
Debug.Print (Months5)
End Sub

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top