• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Regular Expression to capture two numbers using: ("(\d+),(\d+)")

To: All

Giving a string like (56, 78) or (342, 45)

I'm having troubles capturing the two numbers seperatly using regular expression

The expression: (\d+),(\d+) works in a regular expression checker but I can't get it to work in VB.NET

0
vaultworld
Asked:
vaultworld
  • 5
  • 2
1 Solution
 
S-TwilleyCommented:
try:

\(\d+\),\(\d+\)

parentheses (or whatever they're called) are special characters in regex, so you have to escape them
0
 
imperial_p79Commented:
check if \(\d+\),\(\d+\) works!
0
 
imperial_p79Commented:
hmm. i was typing that at the same time as you S-Twilley
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
S-TwilleyCommented:
i think it's slightly wrong actually :P, will post up a better one in a sec
0
 
S-TwilleyCommented:
       Dim regBinaryCapture As New Regex("\((?<x>\d+),(?<y>\d+)\)", RegexOptions.IgnorePatternWhitespace)
        Dim testStr As String = "(35,56)"

        Dim matchTest As Match = regBinaryCapture.Match(testStr)

        If matchTest.Success Then
            MsgBox("X:" & matchTest.Groups("x").Value)
            MsgBox("Y:" & matchTest.Groups("y").Value)
        End If
0
 
S-TwilleyCommented:
that's if you want to record the values with a named group.... my previous example was escaping parentheses in the wrong places...

to test for paired digits in brackets:     \(\d+,\d+\)

to group simply:                                \((\d+),(\d+)\)

to group with named group:               \((?<x>\d+),(?<y>\d+)\)
0
 
S-TwilleyCommented:
you could even get away with ignoring the enclosing parenthesis... depending on whether you want to test for them or not i.e.

do you want   "36,54"   to be treated as ok... or is "(36,54)" the only proper structure:


if you want either way to work (with or without parentheses)... then use these:


to test for paired digits in brackets:     \d+,\d+

to group simply:                                (\d+),(\d+)

to group with named group:               (?<x>\d+),(?<y>\d+)

=========

I think that should be ok... the above though will pull out paired digits in any kind of string, like

"ABC DEF  34, 56 DJ DKD JFFF"
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now