tags:

views:

134

answers:

5

Hi i'm doing a seach and replace and need to replace all characters that are NOT a comma ","

how do i search for all characters in any order

eg

string, like , this

becomes

replace,replace,replace,

thanks

M

+3  A: 

Matching any non-comma chars: [^,]+

so in perl: s/[^,]+/replace/g

Nikolai Ruhe
A: 

In Perl, you can do this:

my $string = "string, like , this";
my $replacement = "replace";
print $string, "\n";
$string =~ s/[^,]+/$replacement/g;
print $string, "\n";
James Thompson
assume you meant $replacement
Salgar
Yes, that's right. Fixed it!
James Thompson
A: 

You should enclose the matching text in parenthesis and then replace that for instance search for:

([^,]+)

and then replace:

\1

with

replace
Savvas Dalkitsis
A: 

In vim:

:%s/[^,]\+/replace/g


%            in the whole file
s            substitute
[^,]         match anything except comma
\+           match one or more times
/replace/    replace matched pattern with 'replace'
g            globally (multiple times on the same line)
stefanB
A: 

In ruby that would be:

original = "string, like , this"
substituted = original.gsub(/[^,]+/, 'replace')
bltxd