Page 1 of 1

Regular Expression: using \1 twice

Posted: Mon Oct 15, 2001 5:55 am
by Luoji
Hello,

I want to duplicate a line and modify only a part of it. I used the following regular expressions in POSIX to replace my line but it didn't work:

replace: command ([.]*)
by: command \1\nnewcommand \1

---

Example:

text to replace:
command attributes
command other attributes

expected result:
command attributes
newcommand attributes
command other attributes
newcommand other attributes

result with my regexp:
command
newcommand attributes
command
newcommand other attributes

---

How come that \1 is only applied at the last occurence?

Regards,


Luoji.

Re: Regular Expression: using \1 twice

Posted: Mon Oct 15, 2001 6:23 am
by Jens Hollmann
Very interesting! Don't put "[]" around the "." and it works! I don't know exactly why but the "[]" are at least not necessary because "." already matches any character.

Re: Regular Expression: using \1 twice

Posted: Tue Oct 16, 2001 8:51 am
by Andreas
[.] matches a . and nothing else
while . matches any character

Within [], most special characters are not special any longer!

See Help/Help Topics/Contents/Reference Information/Regular Expressions/Special Characters