[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Group names in regular expressions

dkmd_nielsen

11/15/2006 5:05:00 PM

Given the following regex to parse telephone numbers:

/^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
)?(?<party>[0-9]{4})/

Is there a regex.method to access the parse data by group name? I was
hoping something like the following behavior:

rx = /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
)?(?<party>[0-9]{4})/
md = rx.match('800 325-3535')
puts md['area']+md['exch']+md['party']

I'm not that skilled with regex (yet), so I don't know if I could
extend the regex class with my own method. I'm not sure what all data
is available to me after parsing.

dvn

5 Answers

Paul Lutus

11/15/2006 7:02:00 PM

0

donn@cmscms.com wrote:

> Given the following regex to parse telephone numbers:
>
> /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
> )?(?<party>[0-9]{4})/
>
> Is there a regex.method to access the parse data by group name? I was
> hoping something like the following behavior:
>
> rx = /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
> )?(?<party>[0-9]{4})/
> md = rx.match('800 325-3535')
> puts md['area']+md['exch']+md['party']
>
> I'm not that skilled with regex (yet), so I don't know if I could
> extend the regex class with my own method. I'm not sure what all data
> is available to me after parsing.

For "group names", you can use this basic replacement idea:

[ 1,2,3,4 ].each do |exch|
[ "jones","smith" ].each do |party|
[ 303,404,505 ].each do |area|
array = data.scan(%r{[regex code](#{area})(#{exch})(#{party})[regex
code]})
end
end
end

using this code and suitable data, "array" will contain:

[[area1,exch1,party1],[area2,exch2,party2], ... ]

--
Paul Lutus
http://www.ara...

Daniel Berger

11/15/2006 7:07:00 PM

0


donn@cmscms.com wrote:
> Given the following regex to parse telephone numbers:
>
> /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
> )?(?<party>[0-9]{4})/
>
> Is there a regex.method to access the parse data by group name? I was
> hoping something like the following behavior:
>
> rx = /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
> )?(?<party>[0-9]{4})/
> md = rx.match('800 325-3535')
> puts md['area']+md['exch']+md['party']
>
> I'm not that skilled with regex (yet), so I don't know if I could
> extend the regex class with my own method. I'm not sure what all data
> is available to me after parsing.
>
> dvn

With Ruby's builtin regex engine? No. With Oniguruma (or the Ruby 1.9
branch)? Yes.

http://www.geocities.jp/kosako3/...

Regards,

Dan

David Friedman

6/18/2012 8:05:00 PM

0

In article <ylfkzk80h719.fsf@dd-b.net>,
David Dyer-Bennet <dd-b@dd-b.net> wrote:

> Doug Wickstr?m <nimshubur@comcast.net> writes:
>
> > On Fri, 15 Jun 2012 04:24:37 GMT, djheydt@kithrup.com (Dorothy J
> > Heydt) wrote:
> >
> >>Yeah, but Reagan was never a Superior Man. He was an okay actor;
> >>he went on getting acting jobs because HE COULD TAKE DIRECTION.
> >>He did what the director told him to. This made him an ideal
> >>sockpuppet for a committee.
> >
> > If you say so. Personally, I've never been in the Oval Office,
> > nor have I ever attended a White House staff or Cabinet meeting,
> > so I don't know.
>
> Given that some of the script cards he was handed for those meetings
> have been published, and accounts by people who WERE there have been
> published, it seems like we know pretty clearly what was going on.

How do you distinguish between your theory and the alternative that he
put together a team to do a job bigger than he could do, and often took
the advice given by the people he had chosen?

Also, how do you make Dorothy's view of him consistent with his extended
service as president of his union?

As best I recall, Reagan first came to my notice when he made a very
good speech in support of Goldwater. That could have been from a script.
But the second time was his 1967 debate with Robert Kennedy. My
impression was that he was clearly an abler debater than Kennedy,
probably smarter, and quite a lot of other people seem to have felt that
Reagan won the debate. If the quote I've seen is correct--"After the
debate, Kennedy growled at his aide Frank Mankiewicz, "Who the f--- got
me into this??"," Kennedy thought so too.

Hard to win a debate with cue cards.

--
http://www.daviddfri...
http://daviddfriedman.blo...
_Salamander_: http://tinyurl.c...
_How to Milk an Almond,..._ http://tinyurl.c...

David Friedman

6/20/2012 6:34:00 AM

0

In article <ylfktxy6ei45.fsf@dd-b.net>,
David Dyer-Bennet <dd-b@dd-b.net> wrote:

> David Friedman <ddfr@daviddfriedman.nopsam.com> writes:
>
> > In article <ylfkzk80h719.fsf@dd-b.net>,
> > David Dyer-Bennet <dd-b@dd-b.net> wrote:
> >
> >> Doug Wickström <nimshubur@comcast.net> writes:
> >>
> >> > On Fri, 15 Jun 2012 04:24:37 GMT, djheydt@kithrup.com (Dorothy J
> >> > Heydt) wrote:
> >> >
> >> >>Yeah, but Reagan was never a Superior Man. He was an okay actor;
> >> >>he went on getting acting jobs because HE COULD TAKE DIRECTION.
> >> >>He did what the director told him to. This made him an ideal
> >> >>sockpuppet for a committee.
> >> >
> >> > If you say so. Personally, I've never been in the Oval Office,
> >> > nor have I ever attended a White House staff or Cabinet meeting,
> >> > so I don't know.
> >>
> >> Given that some of the script cards he was handed for those meetings
> >> have been published, and accounts by people who WERE there have been
> >> published, it seems like we know pretty clearly what was going on.
> >
> > How do you distinguish between your theory and the alternative that he
> > put together a team to do a job bigger than he could do, and often took
> > the advice given by the people he had chosen?
>
> Stuff like the amount of time he took off instead of working. And the
> need for detailed instructions for the simplest interactions, especially
> in his second term.
>
> > Also, how do you make Dorothy's view of him consistent with his extended
> > service as president of his union?
>
> Big difference in time. But also, I can easily believe a union
> president being a figurehead, too.
>
> More to the point, that's so inconsistent with his later behavior that
> it suggests his dementia was pretty well established by the time he got
> to be president, and was being run by his "staff".

Certainly not my father's impression, interacting with Reagan.

> > As best I recall, Reagan first came to my notice when he made a very
> > good speech in support of Goldwater. That could have been from a script.
> > But the second time was his 1967 debate with Robert Kennedy. My
> > impression was that he was clearly an abler debater than Kennedy,
> > probably smarter, and quite a lot of other people seem to have felt that
> > Reagan won the debate. If the quote I've seen is correct--"After the
> > debate, Kennedy growled at his aide Frank Mankiewicz, "Who the f--- got
> > me into this??"," Kennedy thought so too.
>
> That debate probably won Kennedy the election, is the version I've
> heard. This is the one where Reagan won if you saw it in person, but
> Kennedy won if you saw it on television, right? Meaning thousands of
> times as many people saw the version Kennedy won? (His media people
> having told him to play to the cameras rather than the live audience.)

In your reality, Robert Kennedy got elected President in 1968? In mine
it was Richard Nixon.

--
http://www.daviddfri...
http://daviddfriedman.blo...
_Salamander_: http://tinyurl.c...
_How to Milk an Almond,..._ http://tinyurl.c...

David Friedman

6/21/2012 12:17:00 AM

0

In article <ylfk8vfheoqu.fsf@dd-b.net>,
David Dyer-Bennet <dd-b@dd-b.net> wrote:

> goldfarb@ocf.berkeley.edu (David Goldfarb) writes:
>
> > In article <ylfktxy6ei45.fsf@dd-b.net>,
> > David Dyer-Bennet <dd-b@dd-b.net> wrote:
> >>That debate probably won Kennedy the election, is the version I've
> >>heard. This is the one where Reagan won if you saw it in person, but
> >>Kennedy won if you saw it on television, right? Meaning thousands of
> >>times as many people saw the version Kennedy won? (His media people
> >>having told him to play to the cameras rather than the live audience.)
> >
> > I'm pretty sure you're thinking of JFK vs. Nixon, rather than
> > RFK vs. Reagan.
>
> Oops, yes, I was.

Making your point irrelevant to mine, which was that, on the evidence of
that debate, he was abler in spontaneous argument than RFK, generally
regarded, especially by liberals (for all I know correctly), as an
unusually intelligent person.

So far as I can tell, your substantive reply to my evidence of Reagan's
competence is that he might have been competent initially, but early in
his first term (if not sooner) he became intellectually incompetent. Is
that a fair summary?

--
http://www.daviddfri...
http://daviddfriedman.blo...
_Salamander_: http://tinyurl.c...
_How to Milk an Almond,..._ http://tinyurl.c...