LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Regular expression question. (http://www.linuxquestions.org/questions/programming-9/regular-expression-question-259521/)

groentebroer 11-26-2004 10:12 AM

Regular expression question.
 
Hi,

I would like to have a *unknown* number of '+' signs with one space.

So "+" should be " "
also "+++" should be " "
and "++++++" should be " ".

I came up with the following:

$ echo "1 + 2 ++ 3 +++++" | sed -e 's/\++/ /g'

But this replaces all double plus signs ("++") with a space.
So "+" is not replaced and "++++" is replaced with to spaces (" ")

I have tried groups and brackets and everything I could come up with.

Has somebody else any idea or solution?

Thanx,

Gijs

Cedrik 11-26-2004 10:23 AM

Your reg is fine, just enable it with the -r switch

$ echo "1 + 2 ++ 3 +++++" | sed -r 's/\++/ /g'

wapcaplet 11-29-2004 09:15 PM

Yeah, the use of '+' (meaning one or more) in an regex is considered an extended notation. To do the same thing without needing extended notation, try:

Code:

echo "1 + 2 +++ 3 ++++++" | sed -e 's/++*/ /g'
This will match a single '+', followed by zero or more additional '+', and replace them with a space.


All times are GMT -5. The time now is 05:19 AM.