user13107
user13107

Reputation: 3479

How to replace all non whitespace characters except one (semicolon)?

I have following regex in perl for replacing all continuous non-whitespace characters

perl -p -i.bak -e 's/^set gamma=\S*/set gamma=GAMMA/' tmp;

If the tmp file contains set gamma=sdjfskdf; #comment then I want to preserve the semicolon along with the comment. But using \S* deletes sdjfskdf;.

What change should I make to the regex?

Upvotes: 0

Views: 365

Answers (2)

osandov
osandov

Reputation: 127

Try s/^set gamma=[^;\s]*/set gamma=GAMMA/

Upvotes: 3

Qtax
Qtax

Reputation: 33918

In your expression you can replace \S* with [^\s;]*, which doesn't match spaces nor ;.

Upvotes: 2

Related Questions