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-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.
>
> >/X(\d)(??{"[\\w\n]{$1}"})/
> >
> >which doesn't work neither... (?)

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


By the way, when writing my question, I found one solution (is there
another one TIMTOWTDI ?):

([^\\n]|\\n){$1}

it works!

Regards,

Phil.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-13-2004

Quoth http://www.velocityreviews.com/forums/(E-Mail Removed) (Philippe Aymer):
> 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).


Maybe /s doesn't correctly propagate into (regex)-runtime-interpolated
strings (this is probably a bug in the regex engine, if it's true); try

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

> > >/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.


CUT AND PASTE CODE. NEVER RETYPE IT.

> So I try this program:
>
> my $string = "DA3xyzB4ab\nc";
>
> print "==>$string<==" . "\n\n";
>
> if ($string =~ /
> D
> (
> A
> (\d)
> (?{ print "===>$2<===\n"; })
> ( (??{ "[\\w\\n]{$2}" }) )


Again you have \w... please say what you mean.

> (?{ 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...


What do you mean, it doesn't work? . is not a metachar inside character
classes, so this matches $5 occurences of "." or "\n". You want

"(?:.|\\n){$5}"

or use (?s) as above.

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Philippe Aymer
Guest
Posts: n/a
 
      10-14-2004
Hi guys,

My error, I didn't know the "." metachar was not available in
character class (ie "[]").

And yes, it seems that /s doesn't correctly propagate into
(regex)-runtime-interpolated strings as in:
/X(\d)(??{".{$1}"})/s
I don't know if this is by design or a bug.

Thanks again for your help. It was much appreciated!

Phil.

Ben Morrow <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Quoth (E-Mail Removed) (Philippe Aymer):
> > 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).

>
> Maybe /s doesn't correctly propagate into (regex)-runtime-interpolated
> strings (this is probably a bug in the regex engine, if it's true); try
>
> /X(\d)(??{"(?s).{$1}"})/s
>
> > > >/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.

>
> CUT AND PASTE CODE. NEVER RETYPE IT.
>
> > So I try this program:
> >
> > my $string = "DA3xyzB4ab\nc";
> >
> > print "==>$string<==" . "\n\n";
> >
> > if ($string =~ /
> > D
> > (
> > A
> > (\d)
> > (?{ print "===>$2<===\n"; })
> > ( (??{ "[\\w\\n]{$2}" }) )

>
> Again you have \w... please say what you mean.
>
> > (?{ 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...

>
> What do you mean, it doesn't work? . is not a metachar inside character
> classes, so this matches $5 occurences of "." or "\n". You want
>
> "(?:.|\\n){$5}"
>
> or use (?s) as above.
>
> Ben

 
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