Regex to Return A matched Pattern

Is there a method to return a matched pattern and not just return if there was a match.

In a given string I would like to return the pattern that was matched in this case
Return Pattern In a Given String: “Order date: [0-9\/]+$”

String = “the Order date: 02/03/2016 is”
Pattern = “Order date: [0-9\/]+$”
return value = “Order date: 02/03/2016”

put what you need between parenthesis.
in your case : Order date: ([0-9\\/]+)
you will get only the date in the match returned by the search function.

If your regex is correct and finds a match, then the entire matched string will be contained in SubExpressionString(0):

dim rx as new RegEx
rx.SearchPattern = “myregexpattern”

dim match as RegExMatch = rx.Search( sourceText )
dim matchedString as String

if match <> nil then
matchedString = match.SubExpressionString(0)

If you utilize grouping in your pattern, then the matched groups can be found in the other indexes of SubExpressionString

Note that that particular pattern will never match that particular string.

yes you need to remove the end $

i understand the ( ) returns the pattern just the way Perl does

thank you fellas.

Kem your right because of the $ ( end of line )
in this case it will never match.

in my true to life example it will match becuase the end of line is immediately follow the date characters.

this string

Payment 	$26.74 USD

Using this Pattern


returns 26.74 USD

the parenthesis in the pattern are around the numbers and \.

Here’s the code

Is the code removing the parenthesis? ( [\d\.] )

Sub CmpRtnPattern(feedstring as string, feetpattern as string) as String() // returns array
dim rx as new RegEx
rx.SearchPattern = feedPattern
dim match as RegExMatch = rx.Search( feedString )
dim c as integer = 0
Dim Limit As Integer = match.SubExpressionCount
Dim rtnMat As String
Dim rtnArr(10) As String

if match <> nil then
while (c < Limit)
MsgBox "CmpRtnPattern: " + str© + " " + match.SubExpressionString©
rtnArr© = match.SubExpressionString©
c = c + 1
Return rtnArr

Return Nil[/code]

I’m not sure why you’re bothering with all the code. If there is a match, you already know there will be one group and SubExpressionCount will be 2. To get the subgroup you want, check SubExpressionString( 1 ) and be done with it, no?

A couple of more things:

  • You don’t need to escape a dot when it’s in a character class. [\\.] is there same as [.].
  • You don’t need to put a single token like \s into a character class.
  • Don’t use \s when you mean a space only, as I think you do in this case. To be precise, you can use \x20 to represent a space.

hi Kem

I am putting all results into an array because in some cases i will have more than one match per line of text

some text xxx x xxxx x xxx xx xxxx x x xxxxxxx
pattern: some text x ([x]+) x ([x]+)

this should return 3 pattern matches, in my case return 3 pattern matches into an array, then return the array,
however it seems that that in array pos (0) stored more than just desired pattern.

what i am not clear on does Regex Match multiples patterns in one string ?
or can it only match and return 1 pattern per string using ( ) which that string will be found in “SubExpressionString(1)” ?

Let me see if I can clear this up with an example. Suppose you had the text “abc” and the pattern “(\w)(\w)(\w)”. After matching, the RegExMatch object will look like this:

.SubExpressionCount = 4 // the whole match + three subgroups
.SubExpressionString( 0 ) = "abc"
.SubExpressionString( 1 ) = "a" // the first subgroup
.SubExpressionString( 2 ) = "b" // the second subgroup
.SubExpressionString( 3 ) = "c" // the third subgroup

If the pattern had been “\w\w\w” instead, SubExpressionCount would be 1 and SubExpressionString( 0 ) would be “abc”. The number of items in SubExpressionString is always SubExpressionCount - 1. Since you define the pattern, you know up front how many subgroups to expect and what they could contain.

Does that help?


(0) is the full string
(1) is the first match ect …

got it now

thank you Kem.