Need RegEx to remove all alphabets from string - c#

I need a regex to move all alphabets from string (A-Z) and (a-z)..everything including any kind of special character should remain intact. I tried #"[^\d]" but it only returns numbers in string.
String : asd!## $%dfdf4545D jasjkd #(*)jdjd56
desired output : !## $%4545 #(*)56

Just replace all undesired characters with an empty string sequence:
string filtered = Regex.Replace(input, "[A-Za-z]", "");

Try the following regular expression:
[^a-zA-Z]
This will match all non-english letters.

Related

Regex to remove multiple consecutive commas and replace with single comma

Given the input string "Test,,test,,,test,test"
and using the following C# snippet I would have expected the duplicate commas to be replaced by a single comma and results in...
"Test,test,test,test"
private static string TruncateCommas(string input)
{
return Regex.Replace(input, #",+", ",");
}
Code was pinched from this answer...
C# replace all occurrences of a character with just a character
But what I am seeing is "Test,,test,,,test,test" as the output from this function.
Do I need to escape the comma in the regex? Or should this regex be working.
Do I need to escape the comma in the regex?
No.
Or should this regex be working.
Yes.
Please construct your test the following way:
void Main()
{
string s = "Test,,test,,,test,test";
string result = TruncateCommas(s);
Console.WriteLine(result);
}
Output
Test,test,test,test

How can I remove all special chars from UTF8 text in c#?

I would like to remove all special characters from my UTF8 text, but I can't find any matching regular expression.
My text like this:
ASDÉÁPŐÓÖŰ_->,.!"%=%!HMHF
I would like to remove only these chars: _->,.!"%=%!
I tried this regex:
result = Regex.Replace(text, #"([^a-zA-Z0-9_]|^\s)", "");
But it removes my uft8 chars also.
I don't want to remove the accented characters, but I want to remove all glyph.
Regex.Replace(text, #"([^\w]|_)", "")
you want only numbers and letters?
then this is your solution:
result = Regex.Replace(text, "[^0-9a-zA-Z]+", "");
you could also try to specify a range in the ASCII table if you want a custom way of things stay in your string:
result = Regex.Replace(text, "[^\x00-\x80]+", "");

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", "");

how to split the string in c#

I want to split the string
"This is regarding the problem of {pro} in {statement}"
I want to get output is
This is regarding the problem of
{pro}
in
{statement}
You could try this regex:
([^{]+|{[^}]*})
It matches each group of characters which are defined by either:
A sequence of characters (at least one), none of which are {; or
A { character, followed by any number of characters which are not }, all followed by }
Here's a simple regex that will insert a new line before and after your token matches by overriding the evaluator:
string output = Regex.Replace(input, #"{\S+}", m => string.Format(#"{1}{0}{1}", m.Value, '\n'));
The output variable will have a newline after. You can then just do a string split if you need the output in an array of strings.
string[] lines = output.Split('\n');

RegEx.Replace to Replace Whole Words and Skip when Part of the Word

I am using regex to replace certain keywords from a string (or Stringbuilder) with the ones that I choose. However, I fail to build a valid regex pattern to replace only whole words.
For example, if I have InputString = "fox foxy" and want to replace "fox" with "dog" it the output would be "dog dogy".
What is the valid RegEx pattern to take only "fox" and leave "foxy"?
public string Replace(string KeywordToReplace, string Replacement) /
{
this.Replacement = Replacement;
this.KeywordToReplace = KeywordToReplace;
Regex RegExHelper = new Regex(KeywordToReplace, RegexOptions.IgnoreCase);
string Output = RegExHelper.Replace(InputString, Replacement);
return Output;
}
Thanks!
Regexes support a special escape sequence that represents a word boundary. Word-characters are everything in [a-zA-Z0-9]. So a word-boundary is between any character that belongs in this group and a character that doesn't. The escape sequence is \b:
\bfox\b
Do not forget to put '#' symbol before your '\bword\b'.
For example:
address = Regex.Replace(address, #"\bNE\b", "Northeast");
# symbol ensures escape character, backslash(\), does not get escaped!
You need to use boundary..
KeywordToReplace="\byourWord\b"

Categories

Resources