Why isn't my regex removing the whitespace? - c#

I'm using the code below to determine if the value entered into textBoxAddress.Text is a number (fax number) or an email address. My isNumber test below won't work if the fax number has spaces so I'm trying to remove all white space with the Regex, the whitespace isn't being removed. Can you spot my problem?
Thank you.
string faxOrEmail = textBoxAddress.Text;
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", " "); //<---Regex
bool testForFax = IsNumber(faxOrEmail);
if (testForFax == true)
{
//send fax
MessageBox.Show(faxOrEmail);
}
else
{
backgroundWorker1.RunWorkerAsync(); //Send Email
}
static bool IsNumber(string value)
{
// Return true if this is a number.
int number1;
return int.TryParse(value, out number1);
}

You are replacing one or more spaces with a single space.
Regex.Replace(faxOrEmail, #"\s+", string.Empty);
This doesn't remove dashes, and your IsNumber method isn't necessary.
string.Join(string.Empty, faxOrEmail.ToCharArray().Where(x=> char.IsNumber(x)))

Try this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", "");
I don't know C# to well but if you want to remove spaces try replacing it with empty string not space :)

You are replacing the spaces, with spaces! Use String.Empty to avoid these problems:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", String.Empty);

All of the above:
or
Don't use a regular expression at all. Just use the Replace method and replace " " with ""

If you want to replace 2 or more consecutive spaces with one space, then do this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s{2,}", " ");
If you want to remove all spaces, then do this:
faxOrEmail = Regex.Replace(faxOrEmail, #"\s+", string.Empty);

If faxOrEmail only contains spaces (" ") and no tabs for instance, then you can go with:
faxOrEmail = faxOrEmail.Replace(" ", "");
On the other hand, you could also remove everything (including dashes, slashes, parentheses, etc.) but decimal digits with Regex:
faxOrEmail = Regex.Replace(faxOrEmail, "[^0-9]", "");

Related

Find and replace the string in paragraph

I want to empty the value between the hyphn for example need to clear the data in between the range of hyphen prefix and suffix then make it has empty string.
string templateContent = "Template content -macro- -UnitDetails- -testEmail- sending Successfully";
Output
templateContent = "Template content sending Successfully";
templateContent = Regex.Replace(templateContent, #"-\w*-\s?", string.Empty).TrimEnd(' ');
#"-\w*-\s" - is regex pattern for '-Word- '
- - pattern for -
\w - word character.
* - zero or any occurrences of \w
\s - pattern for whitespace character
? - marks \s as optional
TrimEnd(' ') - to remove trailing space if there was a pattern at end of the string
There are many ways to do this, however given your example the following should work
var split = templateContent
.Split(' ')
.Where(x => !x.StartsWith("-") && !x.EndsWith("-"));
var result = string.Join(" ",split);
Console.WriteLine(result);
Output
Template content sending Successfully
Full Demo Here
Note : I personally think regex is better suited to this
You can use regex for this
string regExp = "(-[a-zA-Z]*-)";
string tmp = Regex.Replace(templateContent , regExp, "");
string finalStr = Regex.Replace(tmp, " {2,}", " ");
var resultWithSpaces = Regex.Replace(templateContent, #"-\S+-", string.Empty);
This regular expression looks for two hyphens surrounding one or more characters that are not white space.
It will leave the spaces that were around the removed word. To get rid of those you can do another Regex to replace multiple spaces with a single space.
var result = Regex.Replace(resultWithSpaces, #"\s+", " ");

Remove space from String

I have a lot of strings that look like this:
current affairs
and i want to make the string be :
current affairs
i try to use Trim() but it won't do the job
Regex can do the job
string_text = Regex.Replace(string_text, #"\s+", " ");
You can use regular expressions for this, see Regex.Replace:
var normalizedString = Regex.Replace(myString, " +", " ");
If you want all types of whitespace, use #"\s+" instead of " +" which just deals with spaces.
var normalizedString = Regex.Replace(myString, #"\s+", " ");
Use a regular expression.
yourString= Regex.Replace(yourString, #"\s+", " ");
You can use a regex:
public string RemoveMultipleSpaces(string s)
{
return Regex.Replace(value, #"\s+", " ");
}
After:
string s = "current affairs ";
s = RemoveMultipleSpaces(s);
Using Regex here is the way,
System.Text.RegularExpressions.Regex.Replace(input, #”\s+”, ” “);
This will removes all whitespace characters including tabs, newlines etc.
First you need to split the whole string and then apply trim to each item.
string [] words = text.Split(' ');
text="";
forearch(string s in words){
text+=s.Trim();
}
//text should be ok at this time

Regex to replace invalid characters

I don't have much experience with RegEx so I am using many chained String.Replace() calls to remove unwanted characters -- is there a RegEx I can write to streamline this?
string messyText = GetText();
string cleanText = messyText.Trim()
.ToUpper()
.Replace(",", "")
.Replace(":", "")
.Replace(".", "")
.Replace(";", "")
.Replace("/", "")
.Replace("\\", "")
.Replace("\n", "")
.Replace("\t", "")
.Replace("\r", "")
.Replace(Environment.NewLine, "")
.Replace(" ", "");
Thanks
Try this regex:
Regex regex = new Regex(#"[\s,:.;/\\]+");
string cleanText = regex.Replace(messyText, "").ToUpper();
\s is a character class equivalent to [ \t\r\n].
If you just want to preserve alphanumeric characters, instead of adding every non-alphanumeric character in existence to the character class, you could do this:
Regex regex = new Regex(#"[\W_]+");
string cleanText = regex.Replace(messyText, "").ToUpper();
Where \W is any non-word character (not [^a-zA-Z0-9_]).
Character classes to the rescue!
string messyText = GetText();
string cleanText = Regex.Replace(messyText.Trim().ToUpper(), #"[,:.;/\\\n\t\r ]+", "")
You would probably want to use a whitelist approach, there is an ocean of funny characters whose effect depending on combination may not be easy to figure.
A simple regex that removes everything but the allowed characters could look like this:
messyText = Regex.Replace(messyText, #"[^a-zA-Z0-9\x7C\x2C\x2E_]", "");
The ^ is there to invert the selection, apart from the alphanumeric characters this regex allows | , . and _ You can add and remove characters and character sets as needed.

How to Format a string to be a part of URL?

I need to remove all chars that cant be part of urls, like spaces ,<,> and etc.
I am getting the data from database.
For Example if the the retrieved data is: Product #number 123!
the new string should be: Product-number-123
Should I use regex? is there a regex pattern for that?
Thanks
Here is a an example on how to generate an url-friendly string from a "normal" string:
public static string GenerateSlug(string phrase)
{
string str = phrase.ToLower();
str = Regex.Replace(str, #"[^a-z0-9\s-]", ""); // invalid chars
str = Regex.Replace(str, #"\s+", " ").Trim(); // convert multiple spaces into one space
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
str = Regex.Replace(str, #"\s", "-"); // hyphens
return str;
}
You may want to remove the trim-part if you are sure that you always want the full string.
Source
An easy regex to do this is:
string cleaned = Regex.Replace(url, #"[^a-zA-Z0-9]+","-");
To just perform the replacement of special characters like "<" you can use Server.UrlEncode(string s). And you can do the opposite with Server.UrlDecode(string s).

String.Replace(char, char) method in C#

How do I replace \n with empty space?
I get an empty literal error if I do this:
string temp = mystring.Replace('\n', '');
String.Replace('\n', '') doesn't work because '' is not a valid character literal.
If you use the String.Replace(string, string) override, it should work.
string temp = mystring.Replace("\n", "");
As replacing "\n" with "" doesn't give you the result that you want, that means that what you should replace is actually not "\n", but some other character combination.
One possibility is that what you should replace is the "\r\n" character combination, which is the newline code in a Windows system. If you replace only the "\n" (line feed) character it will leave the "\r" (carriage return) character, which still may be interpreted as a line break, depending on how you display the string.
If the source of the string is system specific you should use that specific string, otherwise you should use Environment.NewLine to get the newline character combination for the current system.
string temp = mystring.Replace("\r\n", string.Empty);
or:
string temp = mystring.Replace(Environment.NewLine, string.Empty);
This should work.
string temp = mystring.Replace("\n", "");
Are you sure there are actual \n new lines in your original string?
string temp = mystring.Replace("\n", string.Empty).Replace("\r", string.Empty);
Obviously, this removes both '\n' and '\r' and is as simple as I know how to do it.
If you use
string temp = mystring.Replace("\r\n", "").Replace("\n", "");
then you won't have to worry about where your string is coming from.
One caveat: in .NET the linefeed is "\r\n". So if you're loading your text from a file, you might have to use that instead of just "\n"
edit> as samuel pointed out in the comments, "\r\n" is not .NET specific, but is windows specific.
What about creating an Extension Method like this....
public static string ReplaceTHAT(this string s)
{
return s.Replace("\n\r", "");
}
And then when you want to replace that wherever you want you can do this.
s.ReplaceTHAT();
Best Regards!
Here is your exact answer...
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
LineFeed
).Replace(mystring, string.Empty);
But this one is much better... Specially if you are trying to split the lines (you may also use it with Split)
const char CarriageReturn = '\r'; // #13
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
string.Format("{0}?{1}", CarriageReturn, LineFeed)
).Replace(mystring, string.Empty);
string temp = mystring.Replace("\n", " ");
#gnomixa - What do you mean in your comment about not achieving anything? The following works for me in VS2005.
If your goal is to remove the newline characters, thereby shortening the string, look at this:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", ""); // new length is 5
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 5);
If your goal is to replace the newline characters with a space character, leaving the string length the same, look at this example:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", " "); // new length is still 6
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 6);
And you have to replace single-character strings instead of characters because '' is not a valid character to be passed to Replace(string,char)
I know this is an old post but I'd like to add my method.
public static string Replace(string text, string[] toReplace, string replaceWith)
{
foreach (string str in toReplace)
text = text.Replace(str, replaceWith);
return text;
}
Example usage:
string newText = Replace("This is an \r\n \n an example.", new string[] { "\r\n", "\n" }, "");
Found on Bytes.com:
string temp = mystring.Replace('\n', '\0');// '\0' represents an empty char

Categories