regular expression, matching sub item

W

wheels619

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!
 
I

it_says_BALLS_on_your forehead

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

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
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.
 
X

Xicheng

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
 
D

Dr.Ruud

(e-mail address removed) 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~
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top