Variables as min,max in regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Frauzel

    Variables as min,max in regular expression

    Quick question.

    I have this regexp:

    s/(.{$descmin,$de scmax} ).*/$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 "informatio n" 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?
  • Steve Grazzini

    #2
    Re: Variables as min,max in regular expression

    David Frauzel <net.weatherson gATnemo> wrote:[color=blue]
    > 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 "informatio n" 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?[/color]

    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

    Comment

    Working...