[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Rexexp Parsing final group of string

dkmd_nielsen

6/25/2006 4:59:00 PM

I'm attempting to parse parameter controls that are in the following
format:

keyword = char(lg) {| | block}

where (lg) and {| | block} are optional parameters. Doesn't seem
difficult. However, I'm having problems capturing the last block.
Some setups catch the block; and some do not. The ones that do not are
the ones that would indicate it is optional.

The following are my test runs. The first three capture the block
within ellipses. Note: I'm using a derivative of the show_regexp
function provided in Dave Thomas' book. The 1/2/3/4 are $1, $2, $3,
and $4. The original expression identified four blocks of code.

=+= The following work fine =+=
======================= Must be there
'priority = h(5) {}', /(\{.*\}[\s,]*)/
priority = h(5) <<{}>>
1:({}) 2:() 3:() 4:()
=======================
======================= Must be at end of string
'priority = h(5) {}', /(\{.*\}[\s,]*)$/
priority = h(5) <<{}>>
1:({}) 2:() 3:() 4:()
=======================
======================= There must be one
'priority = h(5) {}', /(\{.*\}[\s,]*){1}/
priority = h(5) <<{}>>
1:({}) 2:() 3:() 4:()
=======================


These last three fail. These are the ones that say (or I think they
say) "the block may or may not be there." But they fail to identify
the existing block.


=+= The following fail. =+=
======================= Can be zero or more
'priority = h(5) {}', /(\{.*\}[\s,]*)*/
<<>>priority = h(5) {}
1:() 2:() 3:() 4:()
=======================
======================= At least zero, but not more than one
'priority = h(5) {}', /(\{.*\}[\s,]*){0,1}/
<<>>priority = h(5) {}
1:() 2:() 3:() 4:()
=======================
======================= Zero or one occurrence (one I would like to
use)
'priority = h(5) {}', /(\{.*\}[\s,]*)?/
<<>>priority = h(5) {}
1:() 2:() 3:() 4:()
=======================


The entire expression that I thought would work is this:

Regexp.new('(\w+) *= *([A-Za-z]{1,2})(\(\d{1,2}\))*(\{.+\}[\s,]*)*,*')

Thanks for your time and consideration.
dvn

2 Answers

Robert Klemme

6/25/2006 5:28:00 PM

0

dkmd_nielsen@hotmail.com wrote:
> I'm attempting to parse parameter controls that are in the following
> format:
>
> keyword = char(lg) {| | block}
>
> where (lg) and {| | block} are optional parameters. Doesn't seem
> difficult. However, I'm having problems capturing the last block.
> Some setups catch the block; and some do not. The ones that do not are
> the ones that would indicate it is optional.
>
> The following are my test runs. The first three capture the block
> within ellipses. Note: I'm using a derivative of the show_regexp
> function provided in Dave Thomas' book. The 1/2/3/4 are $1, $2, $3,
> and $4. The original expression identified four blocks of code.
>
> =+= The following work fine =+=
> ======================= Must be there
> 'priority = h(5) {}', /(\{.*\}[\s,]*)/
> priority = h(5) <<{}>>
> 1:({}) 2:() 3:() 4:()
> =======================
> ======================= Must be at end of string
> 'priority = h(5) {}', /(\{.*\}[\s,]*)$/
> priority = h(5) <<{}>>
> 1:({}) 2:() 3:() 4:()
> =======================
> ======================= There must be one
> 'priority = h(5) {}', /(\{.*\}[\s,]*){1}/
> priority = h(5) <<{}>>
> 1:({}) 2:() 3:() 4:()
> =======================
>
>
> These last three fail. These are the ones that say (or I think they
> say) "the block may or may not be there." But they fail to identify
> the existing block.
>
>
> =+= The following fail. =+=
> ======================= Can be zero or more
> 'priority = h(5) {}', /(\{.*\}[\s,]*)*/
> <<>>priority = h(5) {}
> 1:() 2:() 3:() 4:()
> =======================
> ======================= At least zero, but not more than one
> 'priority = h(5) {}', /(\{.*\}[\s,]*){0,1}/
> <<>>priority = h(5) {}
> 1:() 2:() 3:() 4:()
> =======================
> ======================= Zero or one occurrence (one I would like to
> use)
> 'priority = h(5) {}', /(\{.*\}[\s,]*)?/
> <<>>priority = h(5) {}
> 1:() 2:() 3:() 4:()
> =======================
>
>
> The entire expression that I thought would work is this:
>
> Regexp.new('(\w+) *= *([A-Za-z]{1,2})(\(\d{1,2}\))*(\{.+\}[\s,]*)*,*')

First hint, don't use the string constructor in this case - you make
your life harder than necessary. Rather use a literal regexp.

Here's what I'd do:

irb(main):001:0> s='priority = h(5) {}'
=> "priority = h(5) {}"

irb(main):008:0>
%r{(\w+)\s*=\s*(\w+)\s*(?:(\([^)]+\))\s*)?(\{[^\}]*\})?}.match(s).to_a
=> ["priority = h(5) {}", "priority", "h", "(5)", "{}"]
irb(main):009:0>
%r{(\w+)\s*=\s*(\w+)\s*(?:(\([^)]+\))\s*)?(\{[^\}]*\})?}.match("a=b").to_a
=> ["a=b", "a", "b", nil, nil]
irb(main):010:0>
%r{(\w+)\s*=\s*(\w+)\s*(?:(\([^)]+\))\s*)?(\{[^\}]*\})?}.match("a=b(c)").to_a
=> ["a=b(c)", "a", "b", "(c)", nil]
irb(main):011:0>
%r{(\w+)\s*=\s*(\w+)\s*(?:(\([^)]+\))\s*)?(\{[^\}]*\})?}.match("a=b
{}").to_a
=> ["a=b {}", "a", "b", nil, "{}"]

You can as well use the form with whitespace and comments to make it
more clear:

%r<
(\w+) # first token
\s*
=
\s*
(\w+) # second token
\s*
(?:(\([^)]+\))\s*)? # optional parens with trailing WS
(\{[^}]*\})? # optional block
>x

Kind regards

robert

DoD

1/5/2011 5:33:00 AM

0

On Jan 4, 11:25 pm, dsharavi <dshara...@hotmail.com> wrote:
> On Jan 4, 9:08 pm, P-Dub <pwolf...@hotmail.com> wrote:
>
>
>
>
>
> > On 1/4/2011 11:15 PM, dsharavi wrote:
>
> > > Arabs Begin to Blame Their Own Leaders for Refugee' Status
> > > Published: 02/22/10, 9:50 AM / Last Update: 02/22/10, 11:17 AM
> > > by Tzvi Ben Gedalyahu
>
> > > Arabs who left Israel in the 1948 are are beginning to blame Arab
> > > countries for leaving them stateless after promising a quick return to
> > > Palestine. "They [Arab leaders] said, 'A week, two weeks,
> > > approximately, and you'll return to Palestine,'" Sadek Mufid, formerly
> > > of Akko (Acre) and now living in Lebanon, recently told Palestinian
> > > Authority television.
>
> > > His comments were translated by Palestinian Media Watch.
>
> > > Saudi Arabia has led an Arab world demand that normalization of ties
> > > with Israel and the establishment of the PA as a state be conditioned
> > > on Israel's allowing the immigration of approximately five million
> > > Arabs. Most of them are descendants of Arabs who claim they used to
> > > live in Israel.
>
> > > The United Nations has classified them as refugees and placed them
> > > in villages, known as camps, in Judea, Samaria and Gaza and Arab
> > > countries. The Arab designation of refugees is maintained today by
> > > the refusal of Arab countries to allow them citizenship, voting
> > > rights, or the ability to move into better housing, in order to
> > > preserve their unique status.
>
> > > Mufid s testimony represents a new trend of Arab leaders, writers and
> > > former Israeli Arab residents who have begun to speak out and openly
> > > blame the Arab leadership for the creation of their situation.
>
> > > Mufid describes a mass departure to Lebanon from Israel, which led to
> > > the creation of "11 or 15 refugee camps." He does not place the blame
> > > on Israel. As Palestinian Media Watch has previously reported, other
> > > recent accounts also describe a deliberate exit from Israel under
> > > orders from Arab leaders, as the Israeli government has always
> > > claimed, which contradicts the Palestinian leadership's charge that
> > > the hundreds of thousands of Arabs who left in 1948 were expelled by
> > > Israel.
>
> > > Mufid, who left the village of Dir Al-Qasi  near Akko in 1948, told
> > > the PA TV's weekly program Returning, "We headed first from Dir al-
> > > Qasi to Rmaich [Lebanon], considering what they [Arab leaders] said at
> > > the time: 'By Allah, in a week or two, you will return to Palestine.'
>
> > > The Arab armies entered Palestine, along with the Arab Liberation
> > > Army. We left - we and those who fled with us - and we all headed for
> > > Lebanon. Some people came to Rmaich and others came to the villages on
> > > the border, such as Ein Ibl and also to Bnit Jibil. People scattered.
> > > And we have about 11 or 15 [refugee] camps in Lebanon."
>
> > > In another PA television interview, an elderly Arab recalled how his
> > > family left Ein Kerem, in eastern Jerusalem. The radio stations of
> > > the Arab regimes kept repeating to us. Get away from the battle
> > > lines. It's a matter of 10 days or two weeks at the most, and we'll
> > > bring you back to Ein Kerem.
>
> > > And we said to ourselves, 'That's a very long time. What is this? Two
> > > weeks? That's a lot!' That's what we thought [then]. And now 50 years
> > > have gone by."
>
> > > Two years ago, Jordanian-based Aryan journalist Jawad Al Bashiti wrote
> > > in Al-Ayyam, The reasons for the Palestinian Catastrophe
> > > [establishment of Israel and the refugee problem] are the same reasons
> > > that have produced and are still producing our catastrophes today...
> > > The first war between Arabs and Israel had started and the Arab
> > > Salvation Army told the Palestinians, We have come to you in order
> > > to liquidate the Zionists and their state. Leave your houses and
> > > villages, you will return to them in a few days safely. Leave them so
> > > we can fulfill our mission in the best way and so you won't be hurt.'
> > > It became clear already then, when it was too late, that the support
> > > of the Arab states [against Israel] was a big illusion.
> > > (IsraelNationalNews.com)
> > >http://www.israelnationalnews.com/News/news.a...
>
> > > Deborah
>
> > This is not new. It is a fact that Arab governments of the time (1948)
> > DEMANDED that all Arab citizens leave Israel 'temporarily' - so that
> > they could push the Jews out, and then they could have their homes back.
>
> > That never happened.
>
> > Today, however - the 'Palestinian' propaganda engine, along with the
> > left-wing idiots - keep insisting that Israel kicked these people out.
> > This, of course, is a big lie - among many Arab/Palestinian lies.
>
> Their lies are so numerous it's hard to keep track. Now they're
> blaming their leaders for telling them to get out -- and this after
> decades and decades of claiming they were all driven out at gunpoint.'
>
> Some didn't claim that, you know. They stated over the years that they
> left in response to their leaders' urging. Naturally, it was all held
> to be lies. Hah.
>
>
>
> > If the so-called Palestinians want peace - and a land of their own -
> > they will have to NEGOTIATE. This means that THEY WILL HAVE TO GIVE UP
> > SOME OF THEIR DEMANDS.
>
> They will also have to start being honest -- and keeping to the
> agreements they've already made. That might prove an insurmountable
> obstacle for them.
>
>
>
> > Since they will never negotiate, then there never again will be a
> > country called Palestine. They can continue to rot in the refugee camps
> > that THEY MADE.
>
> > P-Dub: Israel is forever. Deal with it.
>
> In case you haven't looked too closely at the so-called 'refugee'
> camps, they don't look like they're rotting overmuch.

All these fools can't keep their lies straight. Some claim one thing
one day
and another a different day. I see the Khazar bullshit is pushed
pretty heavy in the
last couple of days.

Curious bunch of malcontents we have here. I used to wonder what makes
them tick,
now I don't really care to know.