regular expression, matching sub item

Discussion in 'Perl Misc' started by wheels619@gmail.com, Jan 30, 2006.

  1. Guest

    Hi,
    I have a quick reg ex question. I'm trying to do a search and replace
    on:
    4in'>1.
    with
    4in'><a name='1'></a>1.

    I'm using this regex:
    s/4in'>[0-9]+\./<a name='$1'><\/a>$1\./g

    I thought that the $1 would match the first match in the brackets, in
    this case, the 1. However, it just says not initialized value.

    Any ideas what special Perl variable to use?
    I tried $+ but same error. $& contains entire match, not just the
    number.

    thanks!
    , Jan 30, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > I have a quick reg ex question. I'm trying to do a search and replace
    > on:
    > 4in'>1.
    > with
    > 4in'><a name='1'></a>1.
    >
    > I'm using this regex:
    > s/4in'>[0-9]+\./<a name='$1'><\/a>$1\./g
    >
    > I thought that the $1 would match the first match in the brackets, in
    > this case, the 1. However, it just says not initialized value.
    >
    > Any ideas what special Perl variable to use?
    > I tried $+ but same error. $& contains entire match, not just the
    > number.


    surround what you want matched in parens.
    it_says_BALLS_on_your forehead, Jan 30, 2006
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead wrote:
    > wrote:
    > > Hi,
    > > I have a quick reg ex question. I'm trying to do a search and replace
    > > on:
    > > 4in'>1.
    > > with
    > > 4in'><a name='1'></a>1.
    > >
    > > I'm using this regex:
    > > s/4in'>[0-9]+\./<a name='$1'><\/a>$1\./g
    > >
    > > I thought that the $1 would match the first match in the brackets, in
    > > this case, the 1. However, it just says not initialized value.
    > >
    > > Any ideas what special Perl variable to use?
    > > I tried $+ but same error. $& contains entire match, not just the
    > > number.

    >
    > surround what you want matched in parens.


    sorry, i meant surround what you want *captured* in parens.
    it_says_BALLS_on_your forehead, Jan 30, 2006
    #3
  4. Xicheng Guest

    wrote:
    > Hi,
    > I have a quick reg ex question. I'm trying to do a search and replace
    > on:
    > 4in'>1.
    > with
    > 4in'><a name='1'></a>1.
    >
    > I'm using this regex:
    > s/4in'>[0-9]+\./<a name='$1'><\/a>$1\./g

    you need parenthesis to capture the matched substrings with $1, $2
    ......, so you may need to do somehing like:

    s#(4in'>)(\d+)\.#$1<a name='$2'></a>$2.#g;
    or
    s#4in'>(\d+)\.#4in'><a name='$1'></a>$1.#g;

    Also, if you include the string '4in'>' in your pattern, you have to
    add it in the "replacement" part, otherwise it'll be removed from the
    resulting string.

    Xicheng

    >
    > I thought that the $1 would match the first match in the brackets, in
    > this case, the 1. However, it just says not initialized value.
    >
    > Any ideas what special Perl variable to use?
    > I tried $+ but same error. $& contains entire match, not just the
    > number.
    >
    > thanks!
    Xicheng, Jan 30, 2006
    #4
  5. Dr.Ruud Guest

    schreef:

    > I'm trying to do a search and replace on:
    > 4in'>1.
    > with
    > 4in'><a name='1'></a>1.



    s~(4in'>)(\d+)(\.)~$1<a name='$2'></a>$2$3~

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jan 31, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,289
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    846
    Alan Moore
    Dec 2, 2005
  3. Frank Wallingford

    Sub-sub-expression evaluation order

    Frank Wallingford, Dec 9, 2004, in forum: C Programming
    Replies:
    4
    Views:
    346
    Chris Torek
    Dec 10, 2004
  4. Ben
    Replies:
    2
    Views:
    884
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,021
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page