3m 2h
[....]
Is there a smart regexp one liner that could produce
[2, 3]
If you want to get [2,3] in both cases, that will be really difficult.
As far as I know, you can only do that in C#, which has named capturing
groups. In all the other languages I know, the capturing groups are
numbered when they are found... That rules it out.
By the way, would it be difficult to implement named capturing groups
in regular expressions ? Would that interest someone ?
Not so difficult, but it's not, as far as I can see, a
one liner. I am working something up at the moment
using an array of regexps.
--- Vincent Fourmond <vincent.fourmond@9online.fr>
wrote:
···
Hello
> I have a string of the form
>
> 2h 3m
>
> or
>
> 3m 2h
> [....]
> Is there a smart regexp one liner that could
produce
>
> [2, 3]
If you want to get [2,3] in both cases, that will
be really difficult.
As far as I know, you can only do that in C#, which
has named capturing
groups. In all the other languages I know, the
capturing groups are
numbered when they are found... That rules it out.
By the way, would it be difficult to implement
named capturing groups
in regular expressions ? Would that interest someone
?
Cheers !
Vince
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
3m 2h
[....]
Is there a smart regexp one liner that could produce
[2, 3]
If you want to get [2,3] in both cases, that will be really difficult.
As far as I know, you can only do that in C#, which has named capturing
groups. In all the other languages I know, the capturing groups are
numbered when they are found... That rules it out.
Well, just to contradict myself, although this is no one-liner:
def scan(str)
re = Regexp.new(/(\d+)h.*(\d+)m|(\d+)m.*(\d+)h/)
if m = re.match(str)
return [m[1], m[2]] if m[1]
return [m[4], m[3]]
end
end
> I have a string of the form
>
> 2h 3m
>
> or
>
> 3m 2h
> [....]
> Is there a smart regexp one liner that could produce
>
> [2, 3]
If you want to get [2,3] in both cases, that will be really difficult.
As far as I know, you can only do that in C#, which has named capturing
groups. In all the other languages I know, the capturing groups are
numbered when they are found... That rules it out.
3m 2h
[....]
Is there a smart regexp one liner that could produce
[2, 3]
If you want to get [2,3] in both cases, that will be really difficult.
As far as I know, you can only do that in C#, which has named capturing
groups. In all the other languages I know, the capturing groups are
numbered when they are found... That rules it out.
Well, just to contradict myself, although this is no one-liner:
def scan(str)
re = Regexp.new(/(\d+)h.*(\d+)m|(\d+)m.*(\d+)h/)
if m = re.match(str)
return [m[1], m[2]] if m[1]
return [m[4], m[3]]
end
end
Python regexps have named capturing groups. It's extremely helpful if you need to construct complicated patterns; because the index of each capturing group can eaasily change when you add and remove things in the regexp.
Tom
···
On Sep 30, 2006, at 2:55 AM, Relm wrote:
As far as I know, you can only do that in C#, which has named capturing
groups. In all the other languages I know, the capturing groups are
numbered when they are found... That rules it out.
But, of course, that *won't* capture "3m 2h", like you described...
True...
So:
r = Regexp.new(/(\d+)h?m?.*(\d+)m?h?/)
'Course, then you'll have [3, 2] for the edge case rather than [2,
3]...but to get the full functionality that the OP described (including
the case where just "2" is given), you'd need fancier logic than just
regexp anyhow.