Replace all non-word characters with a space - c#

I have a regex
[A-Za-z]
and a string, such as
Hi! This is a string.
I want to replace all charcters that are not in the Regex with space. So, I'll end up with
Hi This is a string
How is this done?

var cleaned = Regex.Replace(given, "[^A-Za-z]", " ");

Try:
string output = Regex.Replace(input, "[^A-Za-z]", " ");

Related

Regex to match only numbers , no apostrophes

I want to match only numbers in the following string
String : "40’000"
Match : "40000"
basically tring to ignore apostrophe.
I am using C#, in case it matters.
Cant use any C# methods, need to only use Regex.
Replace like this it replace all char excpet numbers
string input = "40’000";
string result = Regex.Replace(input, #"[^\d]", "");
Since you said; I just want to pick up numbers only, how about without regex?
var s = "40’000";
var result = new string(s.Where(char.IsDigit).ToArray());
Console.WriteLine(result); // 40000
I suggest use regex to find the special characters not the digits, and then replace by ''.
So a simple (?=\S)\D should be enough, the (?=\S) is to ignore the whitespace at the end of number.
DEMO
Replace like this it replace all char excpet numbers and points
string input = "40’000";
string result = Regex.Replace(input, #"[^\d^.]", "");
Don't complicate your life, use Regex.Replace
string s = "40'000";
string replaced = Regex.Replace(s, #"\D", "");

Regular expression doesn't match like I want

So, I got an regular expression :
(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])
That should found all letters and replace it with a blank.
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
But when I got for example :
45a, nomDoc become 45 a, while I juste want 45
Did I write this regex wrong? I'm not very good at it, but I was thinking I was good for this one.
The regex must replace all non-numeric characters, following a numeric character or all non-numeric char before numeric.
45a or a45 must give me 45.
Thank you.
What you're doing, is searching for a spot where the string changes from digits to letters or from letters to digits and insert a space there. So yes, 45a becomes 45 a.
If you want to replace all letters with a blank, use
var nomDoc = Regex.Replace(arr[0], "[A-Za-z]", " ");
But I doubt that this is what you want.
If you want to remove all letters, replace with an empty string instead of a space.
If you want to replace all letters following a digit with a space, use
var nomDoc = Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");
Try this:
var str = "1 oo 23ksls 4910fsj2jd43ld fkkd ^&?&;#";
var nomDoc = str.Replace('/([^0-9]|\n)/g', ' ');
This replaces all the non-number characters(letters, whitespaces and characters) with a space.
You could try this :
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
If you are using Javascript, here's a fiddle :
var Str = "blablabla22445543__-_-_-_-_-_-èèpzofez5zsqef*f-e+ffnfuf'3";
var nomDoc = Str.replace(/[^0-9]/g, "");
$("#result").html(nomDoc);
http://jsfiddle.net/ZqF6L/
It's not quite clear if you want to replace all non-numeric characters with spaces or just remove then completely.
Depending on that, either
var nomDoc = Regex.Replace(arr[0], "[^0-9]", " ");
or
var nomDoc = Regex.Replace(arr[0], "[^0-9]", "");
should do what you want.
hey if you want to remove all your words then use below format method
var demo= Regex.Replace(arr[0], "(?<=[0-9])[A-Za-z]+", " ");

Replace - if preceeded by alphabet

I want to replace hyphen character with space if it is NOT enclosed by digits on both sides.
string str = "Hefer 789-567 dfg-5mh";
str = Regex.Replace(str, #"[a-zA-Z]\-(\d+)", "$1");
Output
Hefer 789-567 df5mh
Desired output
Hefer 789-567 dfg 5mh
You can use negative lookahead and lookbehind: (?<!\d)-|-(?!\d) says "match a - that is not preceeded by a \d or a - that is not followed by a \d".
Thus your regex would be something like
string str = "Hefer 789-567 dfg-5gh";
str = Regex.Replace(str, #"(?<!\d)-|-(?!\d)", " ");
Edit: Note that this also replaces hyphens at the start or end of the string. If you want to avoid this you can use (?<!\d|^)-(?=.)|(?<=.)-(?!\d|$) or (?<=[^\d])-(?=.)|(?<=.)-(?=[^\d]).
The problem you are describing in your title can be solved using this:
Regex.Replace(str, #"(?<=[A-Za-z])-", " ");
The problem you are describing in the body of your question can be solved using this:
Regex.Replace(str, #"(?<!\d)-|-(?!\d)", " ");
Or without lookaround:
Regex.Replace(str, #"([^\d])-|-([^\d])", "$1 $2");

How to convert a space into non breaking space entity in c#

I want to convert more that spaces in a string to through c#?
Like if string is
My name is this.
then output should be
My name is this.
Replace the "regular" space with the "non-breaking space" Unicode character:
string outputString = "Input text".Replace(" ", "\u00A0");
Try with RegEx if you need to convert multiple spaces to a single non-breaking-space:
string convertedText =
new Regex("[ ]{2,}").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
UPDATE
If you need to preserve extra spaces (and to replace with nbsp only multiple spaces) you may use this regex:
string convertedText =
new Regex(" (?= )|(?<= ) ").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
For the second case, as alternative, you may even do not use regex at all (just loop) but they should be faster if you have to do it often with the same regex.
Correction the line below will not work
Please use Server.HtmlEncode for it
You will have to do it by code
string s = " ";
if(s == " ")
{
s = " "
}
Or use "My name is this".Replace(" ", " ");
Try this
string myString = "My name is this".Replace(" ", " ");

String whitespace

I have a string that will have multiple whitespace characters in it and I'm wanting to seperate each word by 1 whitespace character. Say if the string is "Hi! My name is troy and i love waffles!", I want to trim that so it is "Hi! My name is troy and I love waffles!". How would I do this?
Use the regular expression \s+ (one or more whitespace) with the Regex.Replace method from the System.Text.RegularExpressions namespace:
s = Regex.Replace(s, #"\s+", " ");
If you just want to replace spaces you can change the "\s" to a space "":
s = Regex.Replace(s, #" +", " ");
string.Join(" ","Hi! My name is troy and i love waffles!"
.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries)
.Select (s => s.Trim()))
Try this:
var input = "Hi! My name is troy and i love waffles!";
var output = Regex.Replace(input, #"\s{2,}", string.Empty);
Console.WriteLine(output); //Hi! My name is troy and I love waffles!

Categories

Resources