469,625 Members | 1,137 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Variables as min,max in regular expression

Quick question.

I have this regexp:

s/(.{$descmin,$descmax} ).*/$1.../

The purpose is to trim a long string by adding '...' at the end of a
truncated version (after the last full word), using $descmin and $descmax
to determine how long the truncated string can be.

This doesn't seem to behave as I thought it ought. Using digits it works
quite nicely:

s/(.{40,50} ).*/$1.../

But introducing the variables seems to confuse the regexp - although I'm
not sure. The documentation (perlre) doesn't seem to address this (or if
I've missed it, please point me to the relevant paragraph!), and my only
other source of "information" is Komodo's Rx Toolkit, which informs me
that it is attempting to match literal '{', literal 'descmin', literal
'descmax', and the dollars are 'end of string'.

Can I not use variables to describe the min and max in a match count
clause?
Jul 19 '05 #1
1 2348
David Frauzel <net.weathersongATnemo> wrote:
The documentation (perlre) doesn't seem to address this (or if I've
missed it, please point me to the relevant paragraph!), and my only
other source of "information" is Komodo's Rx Toolkit, which informs me
that it is attempting to match literal '{', literal 'descmin', literal
'descmax', and the dollars are 'end of string'.

Can I not use variables to describe the min and max in a match count
clause?


Sure you can. Maybe Komodo is interfering somehow...

$ perl -le '($x,$y) = (1,2); print for "foo" =~ /.{$x,$y}/g'
fo
o

The docs for this (in perlop: the "Gory details of parsing quoted
constructs") confirm that variable interpolation comes before the
compilation of the regex.

--
Steve
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris Lasher | last post: by
4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
7 posts views Thread by Billa | last post: by
25 posts views Thread by Mike | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.