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
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
Matching any non-comma chars: [^,]+
so in perl: s/[^,]+/replace/g
In Perl, you can do this:
my $string = "string, like , this";
my $replacement = "replace";
print $string, "\n";
$string =~ s/[^,]+/$replacement/g;
print $string, "\n";
You should enclose the matching text in parenthesis and then replace that for instance search for:
([^,]+)
and then replace:
\1
with
replace
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)
In ruby that would be:
original = "string, like , this"
substituted = original.gsub(/[^,]+/, 'replace')