regex needed

regex needed

Post by Tim Partridg » Sat, 06 Jul 2002 03:23:26



I don't know regular expressions like the back of my hand yet, but I'm
working on it :) Can someone tell me how to replace a matched pattern with
itself and some extra characters? For example, I want to replace all
instances of [A-Z] with the same thing, but with a spaces around it. So if I
only had "A"s in my text, in vi I could write

:%s/A/ A /g

How can I achieve the same effect if I don't explicitly know what character
to replace? Thanks!

 
 
 

regex needed

Post by John W. Krah » Sat, 06 Jul 2002 04:56:51



> I don't know regular expressions like the back of my hand yet, but I'm
> working on it :) Can someone tell me how to replace a matched pattern with
> itself and some extra characters? For example, I want to replace all
> instances of [A-Z] with the same thing, but with a spaces around it. So if I
> only had "A"s in my text, in vi I could write

> :%s/A/ A /g

> How can I achieve the same effect if I don't explicitly know what character
> to replace? Thanks!

:%s/\([A-Z]\)/ \1 /g

John
--
use Perl;
program
fulfillment

 
 
 

regex needed

Post by Dave Near » Sat, 06 Jul 2002 19:06:37


On Thu, 4 Jul 2002 14:23:26 -0400, Tim Partridge said:

Quote:> I don't know regular expressions like the back of my hand yet, but I'm
> working on it :) Can someone tell me how to replace a matched pattern with
> itself and some extra characters?

In sed:
sed -e 's/\(pattern\)/\1extrastuff/g'

For example, to replace bana with banana, you could do this...
sed -e 's/\(bana\)/\1na/g'
or this...
sed -e 's/\(ba\(na\)\)/\1\2/g'

Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html

 
 
 

regex needed

Post by Barry Margoli » Sun, 07 Jul 2002 01:04:52




>I don't know regular expressions like the back of my hand yet, but I'm
>working on it :) Can someone tell me how to replace a matched pattern with
>itself and some extra characters? For example, I want to replace all
>instances of [A-Z] with the same thing, but with a spaces around it. So if I
>only had "A"s in my text, in vi I could write

>:%s/A/ A /g

>How can I achieve the same effect if I don't explicitly know what character
>to replace? Thanks!

s/pattern/ & /g

'&' in a replacement stands for whatever was matched by the pattern.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.