2

I tried to find the digits (less the first) with this expression, but it works only with the last digit... I know that need capturing a repeat group and not repeating a captured group but I dont understand how is it.

reg:

(\d*)[a-zA-Z]+\d+(?:\.(\d*))*\.[a-zA-Z]+

example

1212asdfasdfdasf101.102.103.asdsadasdasd

1213asdfasdfdasf104.105.106.asdsadasdasd

I want capture 102 and 103, 105, 106, but 1212 and 1213 too. How?? Thanks!

Marc
  • 3,683
  • 8
  • 34
  • 48
eKeR
  • 65
  • 1
  • 9
  • 2
    which language/environment/tool/regex flavor are you using this in? you are still repeating a capturing group (the outer `*` repeats the `(\d*)`). unless you are using .NET, you need a workaround – Martin Ender Jul 02 '13 at 12:24
  • I use visual basic 2008, the best opcion that I found was to get .102.103 like a capturing group and more later another group capturing but only with the script ".102.103." But it is two times using regex, maybe there is a form with only one regex – eKeR Jul 02 '13 at 12:40
  • so we are talking .NET? in this case, have a look at the `Group` object returned for your second capturing group. it will have a property `Captures` in which you can get all numbers – Martin Ender Jul 02 '13 at 12:42
  • Are you sure? The second capturing group only get 103, not 102 and 103 :S – eKeR Jul 02 '13 at 12:53
  • @user2542437: What do you want your output to be from your example text? You want every instance of a digit+ in a group, but you want the entire string to be in one match? More info about how you're using this result may help in finding a solution. – Daniel Gimenez Jul 02 '13 at 13:06
  • @user2542437 don't use `Group.Value`, use `Group.Captures(0).Value` and `Group.Captures(1).Value` – Martin Ender Jul 02 '13 at 13:12

1 Answers1

2

The answer depends on which language you're using.

For most flavours of regex,there is no "simple" answer... For instance, you might think you could do something like this:

^(?:.*?(\d+))+

...Which would (you'd hope) create a new capture group for each group of digits.

However, if you have a quick look at (for example) the java documentation, then you'll see it says:

Capturing groups are numbered by counting their opening parentheses from left to right

i.e. There is a fixed number, as specified by how many pairs of brackets you typed! Thus, in most languages, you'll need to do more than a simple regex match in order to do this job.

That is, unless you can make your regex less generalised (and much more ugly), by doing something horrible like:

^(?:.*?(\d+))?(?:.*?(\d+))?(?:.*?(\d+))?(?:.*?(\d+))?

You can, however, perform this regex match properly, using .NET or Perl 6.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77