I am trying to extract the string <Num> from within Barcode(_<Num>_).PDF using Regex. I am looking at Regular Expression Language - Quick Reference but it is not easy. Thanks for any help.
Dim pattern As String = "^Barcode(_+_)\.pdf"
If Regex.IsMatch("Barcode(_abc123_).pdf", pattern) Then
Debug.Print("match")
End If
If you are trying to not only match but also READ the value of into a variable, then you will need to call the Regex.Match method instead of simply calling the boolean isMatch method. The Match method will return a Match object that will let you get to the groups and captures from your pattern.
Your pattern would need be something like "Barcode\(_(.*)_\)\.pdf"-- note the inner parenthesis which will create a capture group for you to obtain the value of the string between the underscores.. See a MSDN docs for examples of almost exactly what you are doing.
I don't know the regex in VB, but I can offer you a website to examine the correctness of your regex: Regex Tester. In this case, if the <Num> is numbers, you can use "Barcode(_\d+_).pdf"
Just for the record, this is what I ended up using:
'set up regex
'I'm using + instead of * in the pattern to ensure that if no value is
'present the match will fail
Dim pattern As String = "Barcode\(_(.+)_\)\.pdf"
Dim r As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
'get match
Dim mat As Match
mat = r.Match("Barcode(_abc123_).pdf")
'output the matched string
If mat.Success Then
Dim g As Group = mat.Groups(1)
Dim cc As CaptureCollection = g.Captures
Dim c As Capture = cc(0)
Debug.Print(c.ToString)
End If
.NET Framework Regular Expressions
Related
I do not have much RegEx experience and need advice to create a specific Pattern in Excel VBA.
The Pattern I want to match on to validate a Userform field is: nnnnnn.nnn.nn where n is a 0-9 digit.
My code looks like this but Reg.Test always returns false.
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Pattern = "/d/d/d/d/d/d\./d/d/d\./d/d"
End With
If RegEx.Test(txtProjectNumber.Value) = False Then
txtProjectNumber.SetFocus
bolAllDataOK = False
End If
Try this. You need to match the whole contents of the textbox (I assume) so use anchors (^ and $).
Your slashes were the wrong way round. Also you can use quantifiers to simplify the pattern.
Private Sub CommandButton1_Click()
Dim RegEx As Object, bolAllDataOK As Boolean
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Pattern = "^\d{6}\.\d{3}\.\d{2}$"
End With
If Not RegEx.Test(txtProjectNumber.Value) Then
txtProjectNumber.SetFocus
bolAllDataOK = False
End If
End Sub
VBA got it's own build-in alternative called Like operator. So besides the fact you made an error with forward slashes instead of backslashes (as #SJR rightfully mentioned), you should have a look at something like:
If txtProjectNumber.Value Like "######.###.##" Then
Where # stands for any single digit (0–9). Though not as versatile as using regular expressions, it seems to do the trick for you. That way you won't have to use any external reference nor extra object.
I'm VERY new to using regular expressions, and I'm trying to figure something simple out.
I have a simple string, and i'm trying to pull out the 590111 and place it into another string.
HMax_590111-1_v8980.bin
So the new string would simply be...
590111
The part number will ALWAYS have 6 digits, and ALWAYS have a version and such. The part number might change location inside of the string.. so it needs to be able to work if it's like this..
590111-1_v8980_HMXAX.bin
What regex expression will do this? Currently, i'm using ^[0-9]* to find it if it's in the front of the file.
Try the following Regex:
Dim text As String = "590111-1_v8980_HMXAX.bin"
Dim pattern As String = "\d{6}"
'Instantiate the regular expression object.
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
'Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
In Regex \d denotes numerics, so first you write \d.
Then as you know there will be a fix length of numbers which can be specified in Regex with "{}". If you specify \d{6} it means it will expect 6 continuous occurrences of a numeric character.
I would recommend to use this site to try your own expressions. Here you can also find a little bit of information about the expressions you are building if you hover over it.
Regex Tester
I am writing a program that gets lines of input in the following format:
Firstname, Lastname, ID number, contact info
I want to use regex to just grab the ID number which is formatted like A######## where # can be any number.
I have googled and am having trouble understanding VB's regex patterns, can anyone help me out?
In general, there is no such thing as "VB's regex". Both VB.NET and C# uses the same .NET regular expression syntax:
In the .NET Framework, regular expression patterns are defined by a special syntax or language, which is compatible with Perl 5 regular expressions and adds some additional features such as right-to-left matching.
Nevertheless, your Regex should be:
A\d{8}
Which means: Match A, then match any digit (\d) exactly eight times.
Practical VB.NET usage:
Dim input As String = "Firstname, Lastname, A12345678, contact info"
Dim id As String = Regex.Match(input, "A\d{8}").Value
You can simply use:
\bA\d+\b
In context:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim regex As Regex = New Regex("\bA\d+\b")
Dim match As Match = regex.Match("Firstname, Lastname, A123456, Other stuff...")
If match.Success Then
Console.WriteLine(match.Value)
End If
End Sub
End Module
Working example: http://regex101.com/r/pB0pR5
I have strings that look like this
{/CSDC} CHOC SHELL DIP COLOR {17}
I need to extract the value in the first swirly brackets. In the above example it would be
/CSDC
So far i have this code which is not working
Dim matchCode = Regex.Matches(txtItems.Text, "/\{(.+?)\}/")
Dim itemCode As String
If matchCode.Count > 0 Then
itemCode = matchCode(0).Value
End If
I think the main issue here is that you are confusing your regular expression syntax between different languages.
In languages like Javascript, Perl, Ruby and others, you create a regular expression object by using the /regex/ notation.
In .NET, when you instantiate a Regex object, you pass it a string of the regular expression, which is delimited by quotes, not slashes. So it is of the form "regex".
So try removing the leading and trailing / from your string and see how you go.
This may not be the whole problem, but it is at least part of it.
Are you getting the whole string instead of just the 1st value? Regular expressions are greedy by default so .Net is trying to grab the largest matching string.
Try this:
Dim matchCode = Regex.Matches(txtItems.Text, "\{[^}]*\}")
Dim itemCode As String
If matchCode.Count > 0 Then
itemCode = matchCode(0).Groups(0).Value
End If
Edited: I've tried this in Linqpad and it worked.
It appears you are using a capture group.. so try matchCode(0).Groups(0).Value
Also, remove the /\ from the beginning of the pattern and remove the trailing /
I need to loop through all the matches in say the following string:
<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>
I am looking to capture the values in the {} including them, so I want {ProductRowID} and {ProductName}
Here is my code so far:
Dim r As Regex = New Regex("{\w*}", RegexOptions.IgnoreCase)
Dim m As Match = r.Match("<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>")
Is my RegEx pattern correct? How do I loop through the matched values? I feel like this should be super easy but I have been stumped on this this morning!
Your Pattern is missing a small detail:
\{\w*?\}
Curly braces must be escaped, and you want the non-greedy star, or your first (and only) match will be this: "{ProductRowID}'>{ProductName}".
Dim r As Regex = New Regex("\{\w*?\}")
Dim input As String = "<a href='/Product/Show/{ProductRowID}'>{ProductName}</a>"
Dim mc As MatchCollection = Regex.Matches(input, r)
For Each m As Match In mc
MsgBox.Show(m.ToString())
Next m
RegexOptions.IgnoreCase is not needed, because this particular regex is not case sensitive anyway.
You can just group your matches using a regex like the following:
<a href='/Product/Show/(.+)'\>(.+)</a>
In this way you have $1 and $2 matching the values you want to get.
You an also give your matches names so that they aren't anonymous/position oriented for retrieval:
<a href='/Product/Show/(?<rowid>.+)'\>(?<name>.+)</a>
Change your RegEx pattern to \{\w*\} then it will match as you expect.
You can test it with an online .net RegEx tester.