Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Multiple matching with ()* (http://www.velocityreviews.com/forums/t842886-multiple-matching-with.html)

Alessandro Re 07-31-2007 01:34 PM

Multiple matching with ()*
 
Hi there!
I'm Alessandro from Italy and I started using ruby some days ago,
so... Hello, Community! :)

Well, I was trying to match a pattern multiple times. I tried both
with normal match() and scan(), but i can't get the desired result.

The subject string is something like:
"1a2bend" or "beg1a2b3c4dend"
more generally, it should match /^beg(\d\w)*end$/ : always a begin and
ending pattern, and a unspecified number of central pattern.
The problem is that the central pattern must be extracted for every
time it's encountered.
For example, trying with
"x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
returns
[["x", "4D", "z"]]
while i need something like
[["x", "1A", "2B", "3C", "4D", "z"]]

Why does ()* match just the last one? How can i get all the ()* that it matches?

Probabily i'm doing something wrong, but can't understand where :\

Thanks!
--
~Ale


Jano Svitok 07-31-2007 01:48 PM

Re: Multiple matching with ()*
 
On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> Hi there!
> I'm Alessandro from Italy and I started using ruby some days ago,
> so... Hello, Community! :)
>
> Well, I was trying to match a pattern multiple times. I tried both
> with normal match() and scan(), but i can't get the desired result.
>
> The subject string is something like:
> "1a2bend" or "beg1a2b3c4dend"
> more generally, it should match /^beg(\d\w)*end$/ : always a begin and
> ending pattern, and a unspecified number of central pattern.
> The problem is that the central pattern must be extracted for every
> time it's encountered.
> For example, trying with
> "x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
> returns
> [["x", "4D", "z"]]
> while i need something like
> [["x", "1A", "2B", "3C", "4D", "z"]]
>
> Why does ()* match just the last one? How can i get all the ()* that it matches?
>
> Probabily i'm doing something wrong, but can't understand where :\


Try:

if "x1A2B3C4Dz" =~ /^(x)((?:\d\w)*)(z)$/

return [


Jano Svitok 07-31-2007 01:57 PM

Re: Multiple matching with ()*
 
On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> Hi there!
> I'm Alessandro from Italy and I started using ruby some days ago,
> so... Hello, Community! :)
>
> Well, I was trying to match a pattern multiple times. I tried both
> with normal match() and scan(), but i can't get the desired result.
>
> The subject string is something like:
> "1a2bend" or "beg1a2b3c4dend"
> more generally, it should match /^beg(\d\w)*end$/ : always a begin and
> ending pattern, and a unspecified number of central pattern.
> The problem is that the central pattern must be extracted for every
> time it's encountered.
> For example, trying with
> "x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
> returns
> [["x", "4D", "z"]]
> while i need something like
> [["x", "1A", "2B", "3C", "4D", "z"]]
>
> Why does ()* match just the last one? How can i get all the ()* that it matches?
>
> Probabily i'm doing something wrong, but can't understand where :\


Try:

if "x1A2B3C4Dz" =~ /^(x)((?:\d\w)*)(z)$/
a, b = $1, $3 #
return [a] + $2.scan(/\d\w/).flatten + [b]
end

I don't know if it's possible to do it in one run though, maybe you
could use split as well...
Take care when doing nested searches as they will overwrite $1..9
(that's why I used a and b)

J.


Harry Kakueki 07-31-2007 02:01 PM

Re: Multiple matching with ()*
 
On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> For example, trying with
> "x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
> returns
> [["x", "4D", "z"]]
> while i need something like
> [["x", "1A", "2B", "3C", "4D", "z"]]
>

Hi,

Try this.

str = "x1A2B3C4Dz"
p str.scan(/\d?\w/) #>["x", "1A", "2B", "3C", "4D", "z"]

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/


Alessandro Re 07-31-2007 02:09 PM

Re: Multiple matching with ()*
 
Thanks, but i need to match the pattern OR don't match anything.
"lol1a2vasd".scan(/\d?\w/) => ["l", "o", "l", "1a", "2v", "a", "s", "d"]
while i need to be sure that the pattern begins with a regex "x" and
ends with "z"

(of course, x 1 a 2 b 3 c should be regexes not just chars)

thanks, you help is apreciated :)

On 7/31/07, Harry Kakueki <list.push@gmail.com> wrote:
> On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> > For example, trying with
> > "x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
> > returns
> > [["x", "4D", "z"]]
> > while i need something like
> > [["x", "1A", "2B", "3C", "4D", "z"]]
> >

> Hi,
>
> Try this.
>
> str = "x1A2B3C4Dz"
> p str.scan(/\d?\w/) #>["x", "1A", "2B", "3C", "4D", "z"]
>
> Harry
>
> --
> A Look into Japanese Ruby List in English
> http://www.kakueki.com/
>
>



--
~Ale


Alessandro Re 07-31-2007 02:11 PM

Re: Multiple matching with ()*
 
Mh well, to me it seems a normal regex processing (i mean, it *should*
require only one instruction, since this pattern can be read with just
one regex, even if ruby doesn't allow it... but it would be really
bad).
Anyway well, splitting it there are different ways to do it - thanks
for your sudjestion.
But if ruby make it possible with one call, i'd prefer to use it.

Thanks!

On 7/31/07, Jano Svitok <jan.svitok@gmail.com> wrote:
> On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> > Hi there!
> > I'm Alessandro from Italy and I started using ruby some days ago,
> > so... Hello, Community! :)
> >
> > Well, I was trying to match a pattern multiple times. I tried both
> > with normal match() and scan(), but i can't get the desired result.
> >
> > The subject string is something like:
> > "1a2bend" or "beg1a2b3c4dend"
> > more generally, it should match /^beg(\d\w)*end$/ : always a begin and
> > ending pattern, and a unspecified number of central pattern.
> > The problem is that the central pattern must be extracted for every
> > time it's encountered.
> > For example, trying with
> > "x1A2B3C4Dz".scan /^(x)(\d\w)*(z)$/
> > returns
> > [["x", "4D", "z"]]
> > while i need something like
> > [["x", "1A", "2B", "3C", "4D", "z"]]
> >
> > Why does ()* match just the last one? How can i get all the ()* that it matches?
> >
> > Probabily i'm doing something wrong, but can't understand where :\

>
> Try:
>
> if "x1A2B3C4Dz" =~ /^(x)((?:\d\w)*)(z)$/
> a, b = $1, $3 #
> return [a] + $2.scan(/\d\w/).flatten + [b]
> end
>
> I don't know if it's possible to do it in one run though, maybe you
> could use split as well...
> Take care when doing nested searches as they will overwrite $1..9
> (that's why I used a and b)
>
> J.
>
>



--
~Ale


Harry Kakueki 07-31-2007 02:32 PM

Re: Multiple matching with ()*
 
On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> Thanks, but i need to match the pattern OR don't match anything.
> "lol1a2vasd".scan(/\d?\w/) => ["l", "o", "l", "1a", "2v", "a", "s", "d"]
> while i need to be sure that the pattern begins with a regex "x" and
> ends with "z"


str = "lol1a2vasd"
p str.scan(/\d\w|\w{3}/)

Harry

--
A Look into Japanese Ruby List in English
http://www.kakueki.com/


Robert Klemme 07-31-2007 02:56 PM

Re: Multiple matching with ()*
 
2007/7/31, Alessandro Re <akirosspower@gmail.com>:
> Mh well, to me it seems a normal regex processing (i mean, it *should*
> require only one instruction, since this pattern can be read with just
> one regex, even if ruby doesn't allow it... but it would be really
> bad).
> Anyway well, splitting it there are different ways to do it - thanks
> for your sudjestion.
> But if ruby make it possible with one call, i'd prefer to use it.


irb(main):006:0> s="x1A2B3C4Dz"
=> "x1A2B3C4Dz"
irb(main):007:0> s.scan /x(\d\w)*z/
=> [["4D"]]
irb(main):008:0> s.scan /x((?:\d\w)*?)z/
=> [["1A2B3C4D"]]
irb(main):009:0> s.scan(/x((?:\d\w)*?)z/).map {|a| a[0].scan(/\d\w/)}
=> [["1A", "2B", "3C", "4D"]]

Kind regards

robert


Alessandro Re 07-31-2007 03:18 PM

Re: Multiple matching with ()*
 
Thanks, this is an interesting solution!

On 7/31/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> 2007/7/31, Alessandro Re <akirosspower@gmail.com>:
> > Mh well, to me it seems a normal regex processing (i mean, it *should*
> > require only one instruction, since this pattern can be read with just
> > one regex, even if ruby doesn't allow it... but it would be really
> > bad).
> > Anyway well, splitting it there are different ways to do it - thanks
> > for your sudjestion.
> > But if ruby make it possible with one call, i'd prefer to use it.

>
> irb(main):006:0> s="x1A2B3C4Dz"
> => "x1A2B3C4Dz"
> irb(main):007:0> s.scan /x(\d\w)*z/
> => [["4D"]]
> irb(main):008:0> s.scan /x((?:\d\w)*?)z/
> => [["1A2B3C4D"]]
> irb(main):009:0> s.scan(/x((?:\d\w)*?)z/).map {|a| a[0].scan(/\d\w/)}
> => [["1A", "2B", "3C", "4D"]]
>
> Kind regards
>
> robert
>
>



--
~Ale


botp 07-31-2007 03:23 PM

Re: Multiple matching with ()*
 
On 7/31/07, Alessandro Re <akirosspower@gmail.com> wrote:
> Mh well, to me it seems a normal regex processing (i mean, it *should*
> require only one instruction, since this pattern can be read with just
> one regex, even if ruby doesn't allow it... but it would be really bad).


seems like you have a pattern within a pattern.
it may be easy to unwrap outer pattern first, then work on the inner
pattern. something like,

irb(main):096:0> "lol1a2vasd".scan(/lol(.+)asd/).to_s.scan(/\d\w/)
=> ["1a", "2v"]
irb(main):097:0> "beg1a2vend".scan(/beg(.+)end/).to_s.scan(/\d\w/)
=> ["1a", "2v"]
irb(main):098:0> "beg1a2vendxbeg3c4dend".scan(/beg(.+)end/).to_s.scan(/\d\w/)
=> ["1a", "2v", "3c", "4d"]

is that ok?
kind regards -botp



All times are GMT. The time now is 11:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.