Regex remove all special characters except numbers?
Use the global flag:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
Regex to remove all special characters from string?
It really depends on your definition of special characters. I find that a whitelist rather than a blacklist is the best approach in most situations:
tmp = Regex.Replace(n, "[^0-9a-zA-Z]+", "");
You should be careful with your current approach because the following two items will be converted to the same string and will therefore be indistinguishable:
"TRA-12:123"
"TRA-121:23"
Remove all special characters except for @ symbol from string in JavaScript
If you want to keep the @
when it is followed by a word char and keeping the W
is also ok and also remove the newlines, you could for example change the \s
to match spaces or tabs [ \t]
Add the @
to the negated character class and use an alternation specifying to only match the @
when it is not followed by a word character using a negative lookahead.
[^\w \t@]+|@(?!\w)
[^\w \t@]+
Match 1+ times any char except a word char, space or tab|
Or@(?!\w)
Match an @ not directly followed by a word char
Regex demo
In the replacement use an empty string.
How to remove all special characters except for underscore between two words in java?
The RegEx pattern, [^\p{Alnum}_]|^_|_$
meets your requirement.
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of(
"OIL~",
"_OIL_GAS",
"OIL_GAS_",
"_OIL_GAS_",
"*OIL_GAS$",
"OIL_GAS"
).forEach(s -> System.out.println(s.replaceAll("[^\\p{Alnum}_]|^_|_$", "")));
}
}
Output:
OIL
OIL_GAS
OIL_GAS
OIL_GAS
OIL_GAS
OIL_GAS
Explanation of the regex at regex101:
Note: As you must have already understood from the documentation, \p{Alnum}
is same as A-Za-z0-9
.
Python Regex - remove all "." and special characters EXCEPT the decimal point
Use a capturing group to capture only the decimal numbers and at the same time match special chars (ie. not of space and word characters).
Upon replacement, just refer to the capturing group in-order to make use of only the captured chars. ie. the whole match would be removed and replaced by the decimal number if exists.
s = 'What? The Census Says It’s Counted 99.9 Percent of Households. Don’t Be Fooled.'
import re
rgx = re.compile(r'(\d\.\d)|[^\s\w]')
rgx.sub(lambda x: x.group(1), s)
# 'What The Census Says Its Counted 99.9 Percent of Households Dont Be Fooled'
OR
Match all the dots except the one exists between the numbers and all chars except special chars and then finally replace those match chars with an empty string.
re.sub(r'(?!<\d)\.(?!\d)|[^\s\w.]', '', s)
# 'What The Census Says Its Counted 99.9 Percent of Households Dont Be Fooled'
Regex to remove all special characters except periods
You can use
"""[\p{P}\p{S}&&[^.]]+""".toRegex()
The [\p{P}\p{S}&&[^.]]+
pattern matches one or more (+
) punctuation proper (\p{P}
) or symbol (\p{S}
) chars other than dots (&&[^.]
, using character class subtraction).
See a Kotlin demo:
println("a-b)h.".replace("""[\p{P}\p{S}&&[^.]]+""".toRegex(), ""))
// => abh.
How to remove all special Characters from a string except - . and space
Use either \s
or simply a space character as explained in the
Pattern
class javadoc
\s - A whitespace character: [ \t\n\x0B\f\r]
- Literal space character
You must either escape -
character as \-
so it won't be interpreted as range expression or make sure it stays as the last regex character. Putting it all together:
filename.replaceAll("[^a-zA-Z0-9\\s.-]", "")
filename.replaceAll("[^a-zA-Z0-9 .-]", "")
How to remove all special characters from a column name string using regular expressions
Try This:
DECLARE @str VARCHAR(400)='S$d#@gh'
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,(,),.,!]%'
WHILE PATINDEX( @expres, @str ) > 0
SET @str = Replace(REPLACE( @str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),''),'-',' ')
SELECT @str
You can Create a SCALAR FUNCTION & pass this Column
CREATE FUNCTION dbo.Remove_SpecialCharacters( @str VARCHAR(MAX))
RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,(,),.,!]%'
WHILE PATINDEX( @expres, @str ) > 0
SET @str = Replace(REPLACE( @str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),''),'-',' ')
RETURN @str
END
O/P
SELECT dbo.Remove_SpecialCharacters(COLUMNNAME),* FROM TABLENAME
Related Topics
Where Are the Properties.Settings.Default Stored
How to Open in Default Browser in C#
How to Use Task.Delay as a Timer
Getting Error Message Box When Textbox1.Text Is Empty
Call Method in Controller from View(Cshtml)
Thread.Sleep() Without Freezing the Ui
How to Check If an Object Has Changed
Converting a CSV File to Json Using C#
C# Find Highest Array Value and Index
How to Get Url Referrer in ASP.NET Core MVC
How to Convert Word Files to Pdf Programmatically
Fast Way of Finding Most and Least Significant Bit Set in a 64-Bit Integer
String Array to Int Array Conversion
Httpcontext.Current.Request.Files Is Always Empty
Notify When Thread Is Complete, Without Locking Calling Thread
How to Get Name by String Value from a .Net Resource (Resx) File