VBScript - RegEx - modify ObjMatch - Pattern = "(\d{2}) (\d{2}) (\d{2})" - regex

This is something I stumbled across while trying to learn a little about Reg Ex.
Set objRegEx = CreateObject("VBScript.RegExp")
Dim re, targetString, colMatch, objMatch
Set re = New RegExp
With re
.Pattern = "(\d{2}) (\d{2}) (\d{2}) 0500Z"
.Global = True
.IgnoreCase = True
End With
targetString = "02 04 14 0500Z Joe is eating a sandwich"
Set colMatch = re.Execute(targetString)
For each objMatch in colMatch
WScript.echo objMatch
date1 = objRegEx.Replace(objMatch, "(\d{2})(\d{2})(\d{2})")
Wscript.Echo date1
ISSUE: I need to find the date which shows up like this "02 04 14 0500Z" and then assign it to a variable in the form "020414".
When I try to replace the Obj match and reformat the date it doesn't work, instead showing the exact text in brackets.
I referenced:
http://www.mikesdotnetting.com/Article/24/Regular-Expressions-and-VBScript
http://wiki.mcneel.com/developer/scriptsamples/regexpobject

To refer to content captured by capturing group, use $n in the replacement string (where n is a number):
date1 = re.Replace(objMatch, "$1$2$3")
To identify the number of a capturing group, count the number of opening parentheses ( that belongs to a capturing group up to the capturing group you want to refer to:
(\d{2}) (\d{2}) (\d{2}) 0500Z
^ ^ ^
1 2 3
A more complicated example:
((a(?:k)*)(b(c)(?:d)*))
^^ ^ ^
12 3 4
(?:pattern) is a non-capturing group, so it doesn't count.

Related

MS Access Extract all text between brackets to new query

I have a multi-line string variable that holds a large data string. Some of that data is enclosed between square brackets.
Example data variable:
[text 123]
text [text] 234 [blah] blah
some more [text 123]
I need to extract all the data between the square brackets into a query or table, so it would be something like this:
text 123
test
blah
text 123
Here is my VBA code below:
Dim dataString As String
dataString = "test [field 1] mroe text [field 2] etc"
Dim searchStr As String
Dim regExp As Object
Dim colregmatch As MatchCollection
Dim match As Variant
searchStr = dataString
Set regExp = CreateObject("vbscript.regexp")
With regExp
.pattern = "(?<=\[)(.*?)(?=\])"
.IgnoreCase = True
.Global = True
.Multiline = True
End With
Set colregmatch = regExp.Execute(searchStr)
If colregmatch.Count <> 0 Then
For Each match In colregmatch
MsgBox match.Value
Debug.Print match.Value
Next
End If
Set colregmatch = Nothing
Set regExp = Nothing
UPDATE: I get a 5017 run time error when using this pattern. If I use "[([^]]+)]" as the pattern, it works but the brackets are not removed...
The following regex should work:
/(?<=\[).*?(?=\])/gm
See Regex Demo of the regex in action.
Regex Breakdown:
(?<=\[): Positive lookbehind
\[: matches the character [ literally (case sensitive)
.*?: matches any character (except for line terminators) lazily (as few as possible)
(?=\]): Positive lookahead
\]: matches the character ] literally (case sensitive)
gm: global and multi-line modifiers

How to exclude an amount?

I have two strings with the same amount:
Price $22.00
Price Max=$22.00
Can someone please advise how I can modify this regex pattern to make sure that the price with a "Max" in front of it will be ignored?
(?:MAX=|MAX=\s)[$]?[0-9]{0,2}?[,]?[0-9]{1,3}[.][0-9]{0,2}
You may capture the MAX= into an optional capturing group and check if it matched when all matches are found. Only grab the value if the Group 1 did not match:
Dim strPattern As String: strPattern = "(MAX=\s*)?\$\d[\d.,]*"
Dim regEx As Object
Dim ms As Object, m As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.Pattern = strPattern
Dim t As String
t = "Price $24.00 Price Max=$22.00 "
Set ms = regEx.Execute(t)
For Each m In ms
If Len(m.SubMatches(0)) = 0 Then
Debug.Print m.value
End If
Next
The (MAX=\s*)?\$\d[\d.,]* pattern matches MAX= and 0+ whitespaces into an optional group, it matches 1 or 0 times. \$\d[\d.,]* will match a digit and any 0+ digits, commas and dots. If Len(m.SubMatches(0)) = 0 Then will check if Group 1 is not empty, and if yes, the match is valid.
One way to do it could be to match what you don't want and to capture what you do want in a capturing group using an alternation:
Max=\s*\$[0-9]+\.[0-9]+|(\$[0-9]+\.[0-9]+)

RegExp to test Full Name not working as expected

I'm writing a VBA function to evaluate whether a String is a Valid Full Name or not. For example:
Válid Full Name:
David Gilmour
Juan Munoz
Claudio Alberto da Silva
Invalid Full Name:
David Gilm01ur Jr.
Juan Muñoz
Cláudio Alberto da Silva
So the code of my function is this:
Function isVálidoNome(ByVal Texto As String) As Boolean
isVálidoNome = False
'
Dim strPattern As String: strPattern = "(^[a-zA-Z]+(\s?[a-zA-Z])*)*"
'Dim strPattern As String: strPattern = "\d"
Dim regularExpressions As New RegExp
'
regularExpressions.Pattern = strPattern
regularExpressions.Global = True
'
If (regularExpressions.Test(Texto)) Then
isVálidoNome = True
End If
End Function
The pattern I used (^[a-zA-Z]+(\s?[a-zA-Z])*)* works fine in an app I used to test it (RegexPal), but when I run the code in VBA, Strings with digits, accents returns true
Why this problem or Did I make any mistake?
You need to use
^[a-zA-Z]+(?:\s[a-zA-Z]+)*$
See the regex demo
Set regularExpressions.Global = False.
Details:
^ - start of string
[a-zA-Z]+ - 1 or more ASCII letters
(?: - start of a non-capturing group matching zero or more (*) sequences of:
\s - a single whitespace (add + after it to match 1 or more whitespaces)
[a-zA-Z]+
)* - end of the non-capturing group
$ - end of string.

VBA Regex - Grab Hour HH:MM from String

Given a arbitary string I want to grab an hour (HH:MM) from the string.
Here is my regex:
^ # Start of string
(?: # Try to match...
(?: # Try to match...
([01]?\d|2[0-3]): # HH:
)? # (optionally).
([0-5]?\d): # MM: (required)
)? # (entire group optional, so either HH:MM:, MM: or nothing)
$ # End of string
And my code:
Public Sub RegexTest()
Dim oRegex As Object
Dim time_match As Object
Set oRegex = CreateObject("vbscript.regexp")
With oRegex
.Global = True
.Pattern = "^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)$" 'HH:MM
End With
Dim s As String: s = "START TIME: Mar. 3rd 2016 12:00am"
Set time_match = oRegex.Execute(s)
If time_match.Count = 1 Then
Debug.Print time_match.Matches(0)
Else
End If
End Sub
However I am unable to match here and get no output.
Your ^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)$ pattern only matches a full string that starts with an optional HH: part, and and obligatory MM part followed with an obligatory :.
I suggest
(?:[01]?\d|2[0-3]):[0-5]\d
Since you are matching a part of the string.
See regex demo

Can I store the captured groupe strings/digits on a regex search?

I want to store the captured group contents in a regex search to variables
Dim input As String ="asdfd sdf dsf fdsf <disp-formula id=""deqn1-3""> fdsf fds df"
Dim regex As Regex = New Regex("<disp-formula id=""deqn(\d+)-(\d+)"">")
Dim match As Match = regex.Match(input)
If match.Success Then
\\ put the values represented by (\d+) and (\d+) in two variables and then use them in a loop
Can that be done in vb.net? If so how?
Simply use the Groups property of match
Dim g1 = match.Groups(1).Value ' 1 in your sample
Dim g2 = match.Groups(2).Value ' 3 in your sample