Pratik Khadloya
Pratik Khadloya

Reputation: 12869

Regular expression - no capture for negative character class

I am trying to write different sed scripts for beautifying ruby code. One of the cases i am trying to solve is to replace a=>b or a=> b strings with a => b. The regex for matching this condition is [^ ]=> but it also matches 1 character before =>. So, when i try to replace it is not giving me the desired result with s/[^ ]=>/ =>/g

Any suggestions?

Upvotes: 0

Views: 230

Answers (2)

rici
rici

Reputation: 241701

You need to use a capture:

s/\([^ ]\)=>/\1 =>/g

Upvotes: 1

Ry-
Ry-

Reputation: 224904

How about replacing both sides unconditionally?

s/ ?=> ?/ => /g

Upvotes: 0

Related Questions