Ralf Goertz <m...@myprovider.invalid> writes:
> Am Tue, 17 Apr 2018 11:43:36 +0100
> schrieb Ben Bacarisse <
ben.u...@bsb.me.uk>:
>
>> Ralf Goertz <m...@myprovider.invalid> writes:
>>
>> > is it possible to catch multiple matches with the repetition
>> > operators "*", "+" and "{,}"?
>>
>> No. These operators describe a single pattern to be searched for.
>>
>> When they apply to a pattern containing ()s the std:smatch just tells
>> you something about how the pattern was matched -- what was last
>> matched by that sub-expression.
>>
>> There is no provision to store the arbitrary number of matches that
>> might result from one single sub-expression.
>
> Well, that's a pity.
Yes, it can be useful but it's not widely supported. I imagine that's
because it often clearer to do repeated matching and it would complicate
getting the results from otherwise simple patterns (though I suppose it
could be an option).
You can do it in Python with named captures using the extended
(3rd-party) regex module.
<snip>
> One of my real world example (there are many with differing complexity)
> is the following:
>
> some text 4.7 ( 2.3 ) 5.8 (6.2) 4.3 23.4 (2.9)
>
>
> I need the numbers after "some text". There can be any number of numbers
> but they come in pairs with the second parenthesized. However, that second
> number is optional. So my regex looks something like
>
> (([0-9.]+) +(\( *([0-9.]+) *\))?)+$
>
> Of course here I could use a regex without the trailing "+" and match
> repeatedly.
Yes, that's probably what you'll have to do, though in this case you
could just use sscanf or >> (again, in a loop).
> And in all my other use cases I could probably use other
> tricks.
You might be able to generalise the loop into a function so that all
the cases are done in essentially the same way but that's impossible to
tell from here.
--
Ben.