-1

Have a text

hjgv ygkuy (1) erg erg ewr (2)erg erg erg er. (3)

want to get 1,2,3 and if there is only one then just 1

i have tried with (\d)\)

i get matches but i want the result like

1,2,3

if just one then just that digit 1

http://rubular.com/r/xDP7h7IRn7

Cœur
  • 32,421
  • 21
  • 173
  • 232
skcrpk
  • 514
  • 5
  • 17
  • Looks like you just need `\(([0-9])\)` and grab `match.Groups(1).Value`. Do you need to match `(32)`, `(123)`? – Wiktor Stribiżew Jul 11 '17 at 13:49
  • the number can be in 2 or three digits inside the brackets i just need that numbers – skcrpk Jul 11 '17 at 13:52
  • It is common practice to accept the correct solution provided first. Moreover, `\d` [matches more than `0-9` digits in .NET regex](https://stackoverflow.com/a/16621778/3832970). Using non-capturing groups gives you no advantage, but hampers regex execution. Tom's regex is equal to `\((\d+)\)` and is basically the same as mine (but less precise). That is why it is downvoted: it brings no additional value to the already posted solution and is less precise. – Wiktor Stribiżew Jul 11 '17 at 23:50

2 Answers2

1

You may use a simple \(([0-9]+)\) regex that matches any ( followed with 1 or more digits and then a ), while capturing the digits into Group 1.

See a VB.NET demo:

Dim res As MatchCollection = Regex.Matches("hjgv ygkuy (1) erg erg ewr (12)erg erg erg er. (321)", "\(([0-9]+)\)")
If res.Count() > 0 Then
    For Each m As Match In res
        Console.WriteLine(m.Groups(1).Value)
    Next
End If

Output:

1
12
321

Pattern details:

  • \( - a literal (
  • ([0-9]+) - Capturing group 1 matching one or more ASCII digits (as \d in a .NET regex may match more than just 0-9)
  • \) - a literal ).
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

Try using capturing groups;

(?:\()(\d+)(?:\))

Demo

Tom Wyllie
  • 1,840
  • 11
  • 16