I'd like to write a regex that would remove the special characters on following basis:
To remove white space character
#, &, ', (, ), <, > or #
I have written this regex which removes whitespaces successfully:
string username = Regex.Replace(_username, #"\s+", "");
But I'd like to upgrade/change it so that it can remove the characters above that I mentioned.
Can someone help me out with this?
string username = Regex.Replace(_username, #"(\s+|#|&|'|\(|\)|<|>|#)", "");
use a character set [charsgohere]
string removableChars = Regex.Escape(#"#&'()<>#");
string pattern = "[" + removableChars + "]";
string username = Regex.Replace(username, pattern, "");
I suggest using Linq instead of regular expressions:
string source = ...
string result = string.Concat(source
.Where(c => !char.IsWhiteSpace(c) &&
c != '(' && c != ')' ...));
In case you have many characters to skip you can organize them into a collection:
HashSet<char> skip = new HashSet<char>() {
'(', ')', ...
};
...
string result = string.Concat(source
.Where(c => !char.IsWhiteSpace(c) && !skip.Contains(c)));
You can easily use the Replace function of the Regex:
string a = "ash&#<>fg fd";
a= Regex.Replace(a, "[#&'(\\s)<>#]","");
import re
string1 = "12#34#adf$c5,6,7,ok"
output = re.sub(r'[^a-zA-Z0-9]','',string1)
^ will use for except mention in brackets(or replace special char with white spaces) will substitute with whitespaces then will return in string
result = 1234adfc567ok
Related
I have a string as:
string str = "= Fields!Change_Date.Value & Fields!Change_User.Value";
I want the output as:
Change_Date && Change_User
I am able to achieve it with multiple Regex.Replace methods as:
string str = "= Fields!Change_Date.Value & Fields!Change_User.Value";
string x = Regex.Replace(str, #"=? Fields!", " ");
string y = Regex.Replace(x, #".Value", "");
string z = Regex.Replace(y, #"&", "&&");
How Can I achieve this in one go? Is that possible?
You could capture word characters for Change_Date and Change_User and match in between what you don't want to keep.
Then in the replacement use the 2 capture groups only with && in between.
^= Fields!(\w+)\.Value & Fields!(\w+)\.Value$
Explanation
^ Start of string
= Fields! Match literally
(\w+) Capture group 1, match 1+ word characters
\.Value & Fields! Match .Value & Fields!
(\w+) Capture group 2, match 1+ word characters
\.Value Match .Value
$ End of string
See a regex101 demo and a C# demo.
string str = "= Fields!Change_Date.Value & Fields!Change_User.Value";
string result = Regex.Replace(str, #"^= Fields!(\w+)\.Value & Fields!(\w+)\.Value$", "$1 && $2");
Console.WriteLine(result);
Output
Change_Date && Change_User
You could try to search and replace with group capture
var pattern = new Regex(#"[^!]+!([^.]+)[^!]+!([^.]+).*");
var input = "= Fields!Change_Date.Value & Fields!Change_User.Value";
var output = pattern.Replace(input, "$1 && $2");
See demo here
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+", " ");
I want to check multiple words in a string and want to replace them. Suppose that my string is
str= 20148(R)/(work)24553(r)
if(str.contains(("R)" || str.Contains("(work)"))
{
//Here I have to replace (R) and (Work) with space "".
// so that my string should be like this 20148/24553
}
How can check multiple words not by using loops, and in one flow.
I am new to c#. Please help me out
You don't need the if, just do:
var newStr = str.Replace("(R)"," ").Replace("(work)"," ");
If you want a space as you say or:
var newStr = str.Replace("(R)",string.Empty).Replace("(work)",string.Empty);
If you want an empty string.
Put R and r inside a character class to match both letters.
string str = "20148(R)/(work)24553(r)";
string result = Regex.Replace(str, #"\((?:[Rr]|work)\)", "");
Console.WriteLine(result);
IDEONE
OR
string str = "20148(R)/(work)24553(r)";
string result = Regex.Replace(str, #"(?i)\((?:R|work)\)", "");
Console.WriteLine(result);
IDEONE
Pattern Explanation:
(?i) (i modifier) would turn on the case-insensitive mode. So it would match both upper and lowercase letters.
\( Matches a literal ( symbol.
(?:) Non-capturing group.
R|work Matches a letter R or string work.(case-insensitive match)
\) Matches a literal ) symbol.
You could use the Regex.Replace method.
string str = "20148(R)/(work)24553(r)";
string str2 = Regex.Replace(str, "[(](?:R|work)[)]", "", RegexOptions.IgnoreCase);
Console.Writeline(str2); //prints 20148/24553
This says take the string str and match the pattern [(R|work)] and replace any instances with "" ignoring the case of the input string when doing the comparison (so it matches (R) and (r)).
With regex you can replace this
[(]\b(?:R|work)\b[)]
With empty string ""
Edit:
string str1 = "20148(R)/(work)24553(r)";
string str2 = Regex.Replace(str1, "[(]\b(?:R|work)\b[)]", "", RegexOptions.IgnoreCase);
Console.Writeline(str2);
I want to replace all ocurrence of " by \" in a string except if this " is preceded by a \
for exemple the string hello "World\" will become hello \"World\"
Is it possible without using regex ?
But if I have to use regex, what kind have I to use ?
Thanks for help,
regards,
You could use a lookbehind:
var output = Regex.Replace(input, #"(?<!\\)""", #"\""")
Or you could just make the preceeding character optional, for example:
var output = Regex.Replace(input, #"\\?""", #"\""")
This works because " is replaced with \" (which is what you wanted), and \" is replaced with \", so no change.
The regex for this would be:
(?<!\\)"
Without a regex this should do:
yourStringVar.Replace("""","\\""").Replace("\\\\""","\\""");
It is possible without using regex:
str = str.Replace(" \"", "\\\"");
Since you have asked if it's possible without using regex explicitly, that's not as simple and impossible with pure String.Replace approaches. You could use a loop and a StringBuilder:
StringBuilder builder = new StringBuilder();
builder.Append(text[0] == '"' ? "\\\"" : text.Substring(0, 1));
for (int i = 1; i < text.Length; i++)
{
Char next = text[i];
Char last = text[i - 1];
if (next == '"' && last != '\\')
builder.Append("\\\"");
else
builder.Append(next);
}
string result = builder.ToString();
Edit: here's a demo (difficult to create that string literal): http://ideone.com/Xmeh1w
is there an quick way to find(and remove) all escape sequences from a Stream/String??
Hope bellow syntax will be help full for you
string inputString = #"hello world]\ ";
StringBuilder sb = new StringBuilder();
string[] parts = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v','\\' }, StringSplitOptions.RemoveEmptyEntries);
int size = parts.Length;
for (int i = 0; i < size; i++)
sb.AppendFormat("{0} ", parts[i]);
The escape sequences that you are referring to are simply text based represntations of characters that are normally either unprintable (such as new lines or tabs) or conflict with other characters used in source code files (such as the backslash "\").
Although when debugging you might see these chracters represented as escaped characters in the debugger, the actual characters in the stream are not "escaped", they are those actual characters (for example a new line character).
If you want to remove certain characters (such as newline characters) then remove them in the same way you would any other character (e.g. the letter "a")
// Removes all newline characters in a string
myString.Replace("\n", "");
If you are actually doing some processing on a string that contains escaped characters (such as a source code file) then you can simply replace the escaped string with its unescaped equivalent:
// Replaces the string "\n" with the newline character
myString.Replace("\\n", "\n");
In the above I use the escape sequence for the backslash so that I match the string "\n", instead of the newline character.
If you're going for fewer lines of code:
string inputString = "\ncheese\a";
char[] escapeChars = new[]{ '\n', '\a', '\r' }; // etc
string cleanedString = new string(inputString.Where(c => !escapeChars.Contains(c)).ToArray());
You can use System.Char.IsControl() to detect control characters.
To filter control characters from a string:
public string RemoveControlCharacters(string input)
{
return
input.Where(character => !char.IsControl(character))
.Aggregate(new StringBuilder(), (builder, character) => builder.Append(character))
.ToString();
}
To filter control characters from a stream you can do something similar, however you will first need a way to convert a Stream to an IEnumerable<char>.
public IEnumerable<char> _ReadCharacters(Stream input)
{
using(var reader = new StreamReader(input))
{
while(!reader.EndOfStream)
{
foreach(var character in reader.ReadLine())
{
yield return character;
}
}
}
}
Then you can use this method to filter control characters:
public string RemoveControlCharacters(Stream input)
{
return
_ReadCharacters(input)
.Where( character => !Char.IsControl(character))
.Aggregate( new StringBuilder(), ( builder, character ) => builder.Append( character ) )
.ToString();
}
Escape sequense is a string of characters usually beginning with ESC-char but can contain any character. They are used on terminals to control cursor position graphics-mode etc.
http://en.wikipedia.org/wiki/Escape_sequence
Here is my implement with python. Should be easy enough to translate to C.
#!/usr/bin/python2.6/python
import sys
Estart="\033" #possible escape start keys
Estop="HfABCDsuJKmhlp" #possible esc end keys
replace="\015" # ^M character
replace_with="\n"
f_in = sys.stdin
parsed = sys.stdout
seqfile= open('sequences','w')#for debug
in_seq = 0
c = f_in.read(1)
while len(c) > 0 and not c=='\0':
while len(c)>0 and c!='\0' and not c in Estart:
if not c in replace :
parsed.write(c)
else:
parsed.write(replace_with[replace.find(c)])
c = f_in.read(1)
while len(c)>0 and c!='\0' and not c in Estop:
seqfile.write(c)
c = f_in.read(1)
seqfile.write(c) #write final character
c = f_in.read(1)
f_in.close()
parsed.close()
seqfile.close()