Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using a variable size with the repetition quantifier

Reply
Thread Tools

Using a variable size with the repetition quantifier

 
 
Philippe Aymer
Guest
Posts: n/a
 
      10-07-2004
Hi all,

I'm looking at a PERL regex (if possible) that will be able to use a
repetition quantifier metachar, but the number of repetition is
unknown until runtime.
For example:

X3xyz...

the number 3 give me the number of "repetition" for the next chars
(length of string), something like:

/X(\d)(\w{\1})/

but \1 is not possible within {} the repetition quantifier.

Is there a way to use {} with the repetition number only known from
the regex ?

Thanks,

Phil.
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      10-07-2004
On 7 Oct 2004, Philippe Aymer wrote:

>I'm looking at a PERL regex (if possible) that will be able to use a
>repetition quantifier metachar, but the number of repetition is
>unknown until runtime.
>For example:
>
>X3xyz...
>
>the number 3 give me the number of "repetition" for the next chars
>(length of string), something like:
>
>/X(\d)(\w{\1})/
>
>but \1 is not possible within {} the repetition quantifier.
>
>Is there a way to use {} with the repetition number only known from
>the regex ?


Not exactly. You have to do it by some other means. Here are two
examples:

$str =~ /X(\d)/g and $str =~ /\G(\w{$1})/

and

$str =~ /X(\d)((??{ "\\w{$1}" }))/

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      10-08-2004

Philippe Aymer wrote:
>
> I'm looking at a PERL regex (if possible) that will be able to use a
> repetition quantifier metachar, but the number of repetition is
> unknown until runtime.


In general if you want a regex that adapts itself during its own
execution you want (??{}).

> For example:
>
> X3xyz...
>
> the number 3 give me the number of "repetition" for the next chars
> (length of string), something like:
>
> /X(\d)(\w{\1})/
>
> but \1 is not possible within {} the repetition quantifier.
>
> Is there a way to use {} with the repetition number only known from
> the regex ?


/X(\d)((??{"\\w{$1}"}))/

 
Reply With Quote
 
Philippe Aymer
Guest
Posts: n/a
 
      10-08-2004
Jeff 'japhy' Pinyan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On 7 Oct 2004, Philippe Aymer wrote:
>
> >I'm looking at a PERL regex (if possible) that will be able to use a
> >repetition quantifier metachar, but the number of repetition is
> >unknown until runtime.
> >For example:
> >
> >X3xyz...
> >
> >the number 3 give me the number of "repetition" for the next chars
> >(length of string), something like:
> >
> >/X(\d)(\w{\1})/
> >
> >but \1 is not possible within {} the repetition quantifier.
> >
> >Is there a way to use {} with the repetition number only known from
> >the regex ?

>
> Not exactly. You have to do it by some other means. Here are two
> examples:
>
> $str =~ /X(\d)/g and $str =~ /\G(\w{$1})/
>
> and
>
> $str =~ /X(\d)((??{ "\\w{$1}" }))/


This looks like a good solution if there is nothing after in the
string... But in my case, my regex is longer. I should have give this
info before =(

So for example:

X3xyzA4abc....

and only the number can give me the length of the string I want to
grab.

Thanks again,

Phil.
 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      10-08-2004
On 8 Oct 2004, Philippe Aymer wrote:

>> $str =~ /X(\d)((??{ "\\w{$1}" }))/

>
>This looks like a good solution if there is nothing after in the
>string... But in my case, my regex is longer. I should have give this
>info before =(
>
>So for example:
>
>X3xyzA4abc....
>
>and only the number can give me the length of the string I want to
>grab.


I don't think you actually tried my solution, then.

$str = "X3xyzA4abc";
$str =~ /X(\d)((??{ "\\w{$1}" }))/
and print "$1 -> '$2'\n";

That prints: 3 -> 'xyz'

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart


 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      10-08-2004
Philippe Aymer wrote:

> Jeff 'japhy' Pinyan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
>>On 7 Oct 2004, Philippe Aymer wrote:
>>>
>>>/X(\d)(\w{\1})/
>>>
>>>but \1 is not possible within {} the repetition quantifier.

>>
>> $str =~ /X(\d)/g and $str =~ /\G(\w{$1})/
>>
>> $str =~ /X(\d)((??{ "\\w{$1}" }))/

>
> This looks like a good solution if there is nothing after in the
> string... But in my case, my regex is longer.


Can you explain why you think this is a problem?

 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      10-08-2004
In article <(E-Mail Removed) >,
Philippe Aymer <(E-Mail Removed)> wrote:
>Jeff 'japhy' Pinyan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>> On 7 Oct 2004, Philippe Aymer wrote:
>>
>> >I'm looking at a PERL regex (if possible) that will be able to use a
>> >repetition quantifier metachar, but the number of repetition is
>> >unknown until runtime.
>> >For example:
>> >
>> >X3xyz...
>> >
>> >the number 3 give me the number of "repetition" for the next chars
>> >(length of string), something like:
>> >
>> >/X(\d)(\w{\1})/
>> >
>> >but \1 is not possible within {} the repetition quantifier.
>> >
>> >Is there a way to use {} with the repetition number only known from
>> >the regex ?

>>
>> Not exactly. You have to do it by some other means. Here are two
>> examples:
>>
>> $str =~ /X(\d)/g and $str =~ /\G(\w{$1})/
>>
>> and
>>
>> $str =~ /X(\d)((??{ "\\w{$1}" }))/

>
>This looks like a good solution if there is nothing after in the
>string... But in my case, my regex is longer. I should have give this
>info before =(
>
>So for example:
>
>X3xyzA4abc....
>
>and only the number can give me the length of the string I want to
>grab.
>


If you're trying to grab 'em all, maybe:

$str="X3XyzA4abcd....";

print "$2\n" while $str =~/\D*(\d)((??{ "\\w{$1}" }))/g;


--
Charles DeRykus
 
Reply With Quote
 
Philippe Aymer
Guest
Posts: n/a
 
      10-12-2004
Great guys! Thank you!

I was sure PERL would do it. I was aware of (??{}), but for "simple"
pattern, I didn't know the use of '"' which can be usefull for more
complex regex.

Now, I still have a trouble. Because:

/X(\d)((??{"\\w{$1}"}))/

works, but in my string, I also have to match newline. So I did:

/X(\d)(??{"\\w{$1}"})/s

which doesn't work (seems to apply only to //, not things within
(?..)), then:

/X(\d)(??{"[\\w\n]{$1}"})/

which doesn't work neither... (?)

Any idea ?

Thanks again for your response, quick and clean!

Phil.

Brian McCauley <(E-Mail Removed)> wrote in message news:<ck60m1$549$(E-Mail Removed)>...
> Philippe Aymer wrote:
> >
> > I'm looking at a PERL regex (if possible) that will be able to use a
> > repetition quantifier metachar, but the number of repetition is
> > unknown until runtime.

>
> In general if you want a regex that adapts itself during its own
> execution you want (??{}).
>
> > For example:
> >
> > X3xyz...
> >
> > the number 3 give me the number of "repetition" for the next chars
> > (length of string), something like:
> >
> > /X(\d)(\w{\1})/
> >
> > but \1 is not possible within {} the repetition quantifier.
> >
> > Is there a way to use {} with the repetition number only known from
> > the regex ?

>
> /X(\d)((??{"\\w{$1}"}))/

 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      10-12-2004
On 12 Oct 2004, Philippe Aymer wrote:

>Now, I still have a trouble. Because:
>
>/X(\d)((??{"\\w{$1}"}))/
>
>works, but in my string, I also have to match newline. So I did:
>
>/X(\d)(??{"\\w{$1}"})/s
>
>which doesn't work (seems to apply only to //, not things within
>(?..)), then:


The /s modifier only affects the '.' metacharacter. \w doesn't match \n.

>/X(\d)(??{"[\\w\n]{$1}"})/
>
>which doesn't work neither... (?)


This should work:

/X(\d)((??{ "[\\w\\n]{$1}" }))/

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart


 
Reply With Quote
 
Philippe Aymer
Guest
Posts: n/a
 
      10-13-2004
Jeff 'japhy' Pinyan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> On 12 Oct 2004, Philippe Aymer wrote:
>
> >Now, I still have a trouble. Because:
> >
> >/X(\d)((??{"\\w{$1}"}))/
> >
> >works, but in my string, I also have to match newline. So I did:
> >
> >/X(\d)(??{"\\w{$1}"})/s
> >
> >which doesn't work (seems to apply only to //, not things within
> >(?..)), then:

>
> The /s modifier only affects the '.' metacharacter. \w doesn't match \n.


oups... I should have written:

/X(\d)(??{".{$1}"})/s

that's what I'm using ("xyz" in my example coule be anything, even non
printable char).

> >/X(\d)(??{"[\\w\n]{$1}"})/
> >
> >which doesn't work neither... (?)

>
> This should work:
>
> /X(\d)((??{ "[\\w\\n]{$1}" }))/


ok, I have trouble with my fingers... I'm using ".\\n", but no it's
not working.

So I try this program:

my $string = "DA3xyzB4ab\nc";

print "==>$string<==" . "\n\n";

if ($string =~ /
D
(
A
(\d)
(?{ print "===>$2<===\n"; })
( (??{ "[\\w\\n]{$2}" }) )
(?{ print "===>$3<===\n"; })
)
(
B
(\d)
(?{ print "===>$5<===\n"; })
( (??{ "[\\w\\n]{$5}" }) )
(?{ print "===>$6<===\n"; })
)
/xs) {
print "\n";
print "DATA : =>$1<= " . length($1) . "\n";
print "DATA : =>$4<= " . length($4) . "\n";
}

The second pattern : "[.\\n]{$5}" doesn't work... If I replace "." by
"\\w" for this example it works, but I need to match "." (everything)
not "\w".

Thanks again!

Phil.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a quantifier to substitution mohyeldin55@gmail.com Perl Misc 3 07-29-2008 07:07 PM
problems with regex matching - invalid ranges, quantifier follows nothing, etc. Jack Perl Misc 2 10-04-2006 12:53 AM
nested quantifier or unrecognized escape error Francois Massion Perl Misc 6 06-02-2006 10:38 AM
Quantifier...bigger than 32766...in regex leegee@gmail.com Perl Misc 19 05-07-2006 12:25 PM
Greedy quantifier - Java 0 07-11-2005 01:34 AM



Advertisments