Regex remove all occurrences of multiple characters in a string
Use the much faster translate()
for this simple case:
UPDATE tbl SET text = translate(text, '(;<>)', '');
Every character in the second parameter that has no counterpart in the third parameter is replaced with nothing.
The regular expression solution could look like this:
regexp_replace(text, '[(;<>)]', '', 'g');
Essential element is the 4th parameter 'g'
to replace "globally" instead of just the first match. The second parameter is a character class.
You were on the right track, just a matter of syntax for regexp_replace()
.
Hint on UPDATE
If you don't expect all rows to be changed, I would strongly advise to adapt your UPDATE
statement:
UPDATE tbl
SET text = translate(text, '(;<>)', '')
WHERE text <> translate(text, '(;<>)', '');
This way you avoid (expensive) empty updates. (NULL
is covered automatically in this particular case.)
How to remove all instances of 'a' and 'c' in a string in Javascript?
Alternative syntax:
"abcabc".replace(/[ac]/g, '')
This creates a custom character group.
Removing multiple characters from string and spaces
You seem to be dealing with currency strings. You may follow your blacklist approach and use .replace(/[,\s$]+/g,"")
to remove all occurrences of 1+ commas, whitespaces and dollar symbols. However, once you have a pound, euro, yen, etc. currency symbol, you will have to update the regex.
Using a whitelisting approach is easier, remove all chars that are not on your whitelist, digits and dots:
.replace(/[^0-9.]+/g,"")
See the regex demo.
The [^0-9.]+
matches 1 or more occurrences of any chars other than (the [^...]
is a negated character class that matches the inverse character ranges/sets) digits and a dot.
JS demo:
var nums = ["34 345 324.34 $", "$34,345,324.34"];var rx = /[^0-9.]+/g;for (var s of nums) { console.log(s, "=>", s.replace(rx, ""));}
Replace multiple characters in one replace call
If you want to replace multiple characters you can call the String.prototype.replace()
with the replacement argument being a function that gets called for each match. All you need is an object representing the character mapping that you will use in that function.
For example, if you want a
replaced with x
, b
with y
, and c
with z
, you can do something like this:
const chars = {'a':'x','b':'y','c':'z'};
let s = '234abc567bbbbac';
s = s.replace(/[abc]/g, m => chars[m]);
console.log(s);
Output: 234xyz567yyyyxz
How can I replace multiple characters in a string?
Doesn't seem this even needs regex. Just 2 chained replacements would do.
var str = '[T] and [Z] but not [T] and [Z]';var result = str.replace('T',' ').replace('Z','');console.log(result);
How do I remove multiple characters in a string
I suggest putting the characters you need to remove into a character class:
With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an
a
or ane
, use[ae]
. You could use this ingr[ae]y
to match eithergray
orgrey
.
Code:
PS> $Name = "\Test Name\Test_Underscore 1_2"
PS> $NameFull1 = $NAME -replace '[\s_\\]+'
PS> $NameFull1
TestNameTestUnderscore12
The [\s_\\]
will match either whitespace, _
or \
.
Remove multiple occurrences of a string regex on a file, line by line
You may use:
perl -pe 's/\h+\d+="[^"]*"//g' test.txt
12-09:30:09:802775 |539----> 116 Bl_LE 502450553 | <D BeginString="FIX.4.2" (...) LTPrice="13.21" CheckSum="145"></D>
12-09:30:09:802775 |539----> 116 Bl_LE 502450553 | <D BeginString="FIX.4.2" (...) LTPrice="13.21" CheckSum="145"></D>
RexEx Details:
\h+
: Match 1 or more whitespaces\d+
: Match 1+ digits=
: Match a=
"[^"]*"
: Match a quoted value
Regex to replace multiple occurence of @ except first
You may use
var result = Regex.Replace(text, @"(?<!^[^@]*)@", "");
See the regex demo. Details:
(?<!^[^@]*)
- a negative lookbehind that makes sure there is no@
before the current location up to the string start@
- a@
in other contexts.
In case you do not really have to use a regex,
var result = text.Substring(0, text.IndexOf("@")+1) + text.Substring(text.IndexOf("@")+1).Replace("@", "");
should also work. See the C# demo.
Related Topics
Why SQL Server Go Slow When Using Variables
Multiple Tables Need One to Many Relationship
Create Computed Column Using Data from Another Table
Intersection of Multiple Arrays in Postgresql
Split Date Range into One Row Per Month in SQL Server
Convert Number to Words - First, Second, Third and So On
SQL Server Case .. When .. in Statement
Ssis Connection Not Found in Package
SQL Server After Update Trigger
SQL Unpivot Multiple Columns Data
Identifying Source Table from Union Query
Sql:Using Group by and Max on Multiple Columns
Bigquery - JSON_Extract All Elements from an Array
How to Get Running Sum of a Column in SQL Server