Skip a word in a string along with special character - c#

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

Related

C# Filter a word with an undefined number of spaces between charachers

For exampe:
I can create a wordt with multiple spaces for example:
string example = "**example**";
List<string>outputs = new List<string>();
string example_output = "";
foreach(char c in example)
{
example_putput += c + " ";
}
And then i can loop it to remve all spaces and add them to the outputs list,
The problem there is. I need it to work in scenario's where there are double spaces and more.
For example.
string text = "This is a piece of text for this **example**.";
I basicly want to detect AND remove 'example'
But, i want to do that even when it says e xample, e x ample or example.
And in my scenaria, since its a spamfilter, i cant just replace the spaces in the whole sentence like below, because i'd need to .Replace( the word with the exact same spaces as the user types it in).
.Replace(" ", "");
How would i achieve this?
TLDR:
I want to filter out a word with multiple spaces combinations without altering any other parts of the line.
So example, e xample, e x ample, e x a m ple
becomes a filter word
I wouldn't mind a method which could generates a word with all spaces as plan b.
You can use this regex to achieve that:
(e[\s]*x[\s]*a[\s]*m[\s]*p[\s]*l[\s]*e)
Link
Dotnet Fiddle
You could use a regex for that: e\s*x\s*a\s*m\s*p\s*l\s*e
\s means any whitespace character and the * means 0-n count of that whitespace.
Small snippet:
const string myInput = "e x ample";
var regex = new Regex("e\s*x\s*a\s*m\s*p\s*l\s*e");
var match = regex.Match(myInput);
if (match.Success)
{
// We have a match! Bad word
}
Here the link for the regex: https://regex101.com/r/VFjzTg/1
I see that the problem is to ignore the spaces in the matchstring, but not touch them anywhere else in the string.
You could create a regular expression out of your matchword, allowing arbitrary whitespace between each character.
// prepare regex. Need to do this only once for many applications.
string findword = "example";
// TODO: would need to escape special chars like * ( ) \ . + ? here.
string[] tmp = new string[findword.Length];
for(int i=0;i<tmp.Length;i++)tmp[i]=findword.Substring(i,1);
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(string.Join("\\s*",tmp));
// on each text to filter, do this:
string inp = "A text with the exa mple word in it.";
string outp;
outp = r.Replace(inp,"");
System.Console.WriteLine(outp);
Left out the escaping of regex-special-chars for brevity.
You can try regular expressions:
using System.Text.RegularExpressions;
....
// Having a word to find
string toFind = "Example";
// we build the regular expression
Regex regex = new Regex(
#"\b" + string.Join(#"\s*", toFind.Select(c => Regex.Escape(c.ToString()))) + #"\b",
RegexOptions.IgnoreCase);
// Then we apply regex built for the required text:
string text = "This is a piece of text for this **example**. And more (e X amp le)";
string result = regex.Replace(text, "");
Console.Write(result);
Outcome:
This is a piece of text for this ****. And more ()
Edit: if you want to ignore diacritics, you should modify regular expression:
string toFind = "Example";
Regex regex = new Regex(#"\b" + string.Join(#"\s*",
toFind.Select(c => Regex.Escape(c.ToString()) + #"\p{Lm}*")),
RegexOptions.IgnoreCase);
and Normalize text before matching:
string text = "This is a piece of text for this **examplé**. And more (e X amp le)";
string result = regex.Replace(text.Normalize(NormalizationForm.FormD), "");

Regular expression to replace string except in sqaure brackets

Need to replace all forward-slash (/) with > except for the ones in the square brackets
input string:
string str = "//div[1]/li/a[#href='https://www.facebook.com/']";
Tried pattern (did not work):
string regex = #"\/(?=$|[^]]+\||\[[^]]+\]\/)";
var pattern = Regex.Replace(str, regex, ">");
Expected Result:
">>div[1]>li>a[#href='https://www.facebook.com/']"
Your thinking was good with lookbehind but instead positive use negative.
(?<!\[[^\]]*)(\/)
Demo
After updating your c# code
string pattern = #"(?<!\[[^\]]*)(\/)";
string input = "//div[1]/li/a[#href='https://www.facebook.com/']";
var result = Regex.Replace(input, pattern, ">");
You will get
>>div[1]>li>a[#href='https://www.facebook.com/']
If you're willing to also use String.Replace you can do the following:
string input = "//div[1]/li/a[#href='https://www.facebook.com/']";
string expected = ">>div[1]>li>a[#href='https://www.facebook.com/']";
var groups = Regex.Match(input, #"^(.*)(\[.*\])$")
.Groups
.Cast<Group>()
.Select(g => g.Value)
.Skip(1);
var left = groups.First().Replace('/', '>');
var right = groups.Last();
var actual = left + right;
Assert.Equal(expected, actual);
What this does is split the string into two groups, where for the first group the / is replaced by > as you describe. The second group is appended as is. Basically, you don't care what is between square brackets.
(The Assert is from an xUnit unit test.)
You could either match from an opening till a closing square bracket or capture the / in a capturing group.
In the replacement replace the / with a <
Pattern
\[[^]]+\]|(/)
\[[^]]+\] Match from opening [ till closing ]
| Or
(/) Capture / in group 1
Regex demo | C# demo
For example
string str = "//div[1]/li/a[#href='https://www.facebook.com/']";
string regex = #"\[[^]]+\]|(/)";
str = Regex.Replace(str, regex, m => m.Groups[1].Success ? ">" : m.Value);
Console.WriteLine(str);
Output
>>div[1]>li>a[#href='https://www.facebook.com/']

Regex for removing only specific special characters from string

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

Check multiple words in a string using Contains method

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

Replacing all occurrences of alphanumeric characters in a string

I'm trying to replace all alphanumeric characters in my string with the character "-" using regex. So if the input is "Dune" i should get "----". currently though I'm getting just the single "-";
string s = "^[a-zA-Z0-9]*$";
Regex rgx = new Regex(s);
string s = "dune";
string result = rgx.Replace(s, "-");
Console.WriteLine(result);
Console.Read();
right now i know its looking for the string "dune" rather then the letters "d" "u" "n" "e". but i can find another class that would work.
Your regex is too greedy, remove the * and start end string matches. It should be
string s = "[a-zA-Z0-9]";
This will then only match 1 character anywhere in the string rather than all. You could also look at the shorthand for any alphanumeric
String s= "\w";
Try
string s = "[a-zA-Z0-9]";
Regex rgx = new Regex(s);
string s = "dune";
string result = rgx.Replace(s, "-");
Console.WriteLine(result);
Console.Read();
Why do you have one String s for your regular expression and another String s for your string? I would change this to eliminate confusion/error here.
Also to replace each alphanumeric character, you need to remove the beginning of string/end of string anchors ^ $ and the * quantifier meaning (0 or more times, matching the most amount possible)
Regex rgx = new Regex("[a-zA-Z0-9]");
string s = "dune";
string result = rgx.Replace(s, "-");
Console.WriteLine(result); //=> "----"

Categories