Replace string but only for particular pattern in c# - c#

I have 2 strings, one is main string and other is pattern string to be replaced. I want to replace the some part of main string with pattern string only if particular pattern is matched in main string.
Example:
string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
string pattern = "[lmn].[def].[ijk]";
i want final string as
[lmn].[def].[ijk] = [abc].[def].[ijk]
i.e. if only 2 part is there in string than only replace not for 3 parts
i am using:
mainstring = mainstring.Replace("[def].[ijk]",pattern);
but it replaces as,
[lmn].[def].[ijk] = [abc].[lmn].[def].[ijk]
^-------+-------^
|
+-- don't replace here
but I want as
[lmn].[def].[ijk] = [abc].[def].[ijk]
EDIT: Additional rule for the replacement:
You can touch left hand side or right hand side but the pattern should be alone without anything at before or after.

I am trying to follow this rule:
i.e. if only 2 part is there in string than only replace not for 3 parts
(interpreting it as replace only if the matching pattern is not inbetween other parts, or: neither preceded nor follwed by a .)
This regular expression should satisfy this rule above:
string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
// will replace only right side
//string mainstring = "[abc].[def].[ijk] = [def].[ijk]";
string replacement = "[lmn].[def].[ijk]";
string pattern = #"(?<!\.)\[def\].\[ijk\](?!\.)";
string output = Regex.Replace(mainstring, pattern, replacement);
EDIT:
if you need to transform you basic pattern into a regex format replace both parentheses:
string searchPattern = "[def].[ijk]";
searchPattern = searchPattern.Replace("[", #"\[").Replace("]", #"\]");
and place the result in between the look ahead and look behind pattern:
string pattern = #"(?<!\.)" + searchPattern + #"(?!\.)";
Explanation:
(?<!\.): the pattern is called negative lookahead. It will match only if the preceding character is not a . (dot)
(?!\.): the pattern is called negative lookbehind. It will match only if the following character is not a . (dot)
the replacement of [ ] to \[ \] is necessary because regex treats the parentheses as an interval or group and matches every character between the parentheses. Like [0-9] will match any digit between 0 and 9. But if you use an escape sequence #"\[0-9\]" it will take the parentheses as a character and match the pattern "[0-9]". I hope it became a little clearer

does this work for you?
string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
string leftFillWith = "[lmn].[def].[ijk]";
string pattern = #"((\[lmn\]).)?((\[def\]).)?(\[ijk\])? =";
var expected= Regex.Replace(mainstring, pattern,match=> leftFillWith+" =" );
if only two parts are accepted:
string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
string leftFillWith = "[lmn].[def].[ijk]";
// string pattern = #"((\[lmn\]).)?((\[def\]).)?(\[ijk\])? ="; //if any number of parts is accepted on left
string pattern = #"(\[def\]\.\[ijk\])(\s)+="; //if only two parts are accepted
string expected = Regex.IsMatch(mainstring,pattern)? Regex.Replace(mainstring, pattern, match => leftFillWith + " ="):mainstring;

Not good but work;
string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
string pattern = "[lmn].[def].[ijk]";
string search ="[def].[ijk]";
string mainstring3 = "";
string[] mainstring2 = mainstring.Split(' ');
for (int i = 0; i < mainstring2.Length; i++)
{
if (mainstring2[i] == search)
{
mainstring2[i] = pattern;
}
if (i < mainstring2.Length - 1)
{
mainstring3 += mainstring2[i] + " ";
}
else
{
mainstring3 += mainstring2[i];
}
}
mainstring = mainstring3;
}

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

Replace ab in string |var11=ab|var12=100|var21=cd|var22=200| using regular expression

i want to replace ab followed by var11 in given string
Input:|var11=ab|var12=100|var21=cd|var22=200|
My code is as follows:
string input = "|var11=ab|var12=100|var21=cd|var22=200|";
string pattern = #"^.var11=([a-z]+).";
string value = Regex.Replace(input, pattern, "ep");
and the output I got is:
epvar12=100|var21=cd|var22=200|
but the expected output was:
|var11=ep|var12=100|var21=cd|var22=200|
You may use
string input = "|var11=ab|var12=100|var21=cd|var22=200|";
string pattern = #"(?<=\bvar11=)[^|]+";
string value = Regex.Replace(input, pattern, "ep");
Or, a capturing group approach:
string pattern = #"\b(var11=)[^|]+";
string value = Regex.Replace(input, pattern, "${1}ep");
See the .NET regex demo
Details
(?<=\bvar11=) - a location immediately preceded with a whole word var11=
[^|]+ - 1+ non-pipe chars.
If you want to update the var11 value only when it is preceded with | or at the start of string use
string pattern = #"(?<=(?:^|\|)var11=)[^|]+";
where (?:^|\|) matches start of string (^) or (|) a pipe char (\|).

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); //=> "----"

Regex for a special string pattern on multiple levels

I need to evaluate a string pattern that is something like as given below and I am quite new at writing such complex expressions
<%(ddlValue){DropDownList}[SelectedValue]%>
// this has three part (Control Name) {Control Type} [Control Property]
I tried a whole lot of regex and other tools like RegExr but anything did not worked. I have to do this on four levels, that is as given below in the code. So here is what I have done:
string regex = "/[<%](.*?)[%>]/g"; // Regex to match "<% %>" pattern
Match mtch = Regex.Match(strQuery, regex, RegexOptions.Singleline);
string strControlName = "";
string strControlType = "";
string strControlProp = "";
if (mtch.Success)
{
string strVal = mtch.Value;
Match mtchControlName = Regex.Match(strVal, "/[(]\S)/");
// Regex to match "()" i.e. control name ("ddlValue" in above example)
if (mtchControlName.Success)//Match control Name
{
strControlName = mtchControlName.Value;
Match mtchControlType = Regex.Match(strVal, "/[{]\s[}]/");
// Regex to match "[]" i.e. control type
if (mtchControlType.Success) // Match Control Type
{
strControlType = mtchControlType.Value;
Match mtchControlProp = Regex.Match(strVal, "/[(]\S[)]/");
// Regex to match "[]" i.e. control property
if (mtchControlProp.Success) // Match Control Prop
{
strControlProp = mtchControlProp.Value;
}
}
}
}
You can do this in a single regex. Being as specific as possible, you could do this:
Regex regexObj = new Regex(
#"\( # Match (
( # Capture in group 1:
[^()]* # Any number of characters except ()s
) # End of group 1
\) # Match )
\{([^{}]*)\} # The same for {...} into group 2
\[([^\[\]]*)\] # The same for [...] into group 3",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
Then you case use
Match matchResults = regexObj.Match(subjectString);
to get a Match object. Access the submatches via
matchResults.Groups(n).Value // insert 1, 2 or 3 for n
See it live on regex101.com.
You can use capturing groups in one expression to capture all groups together:
String input = "<%(ddlValue){DropDownList}[SelectedValue]%>";
String pattern = #"<%\((.+)\)\{(.+)\}\[(.+)\]%>";
Match m = Regex.Match(input, pattern);
if (m.Groups.Count == 4)
{
string firstpart = m.Groups[1].ToString();
string secondpart = m.Groups[2].ToString();
string thirdpart = m.Groups[3].ToString();
}
You could use named groups. It makes the code more readable.
Match m = Regex.Match(inputData, #"^<%\((?<ddlValue>[^)]+)\){(?<DropDownList>[^}]+)}\[(?<SelectedValue>[^\]]+)\]%>$");
if (m.Groups.Count == 4)
{
string firstpart = m.Groups["ddlValue"].ToString();
string secondpart = m.Groups["DropDownList"].ToString();
string thirdpart = m.Groups["SelectedValue"].ToString();
}

Replace any string between quotes

Problem:
Cannot find a consistent way to replace a random string between quotes with a specific string I want. Any help would be greatly appreciated.
Example:
String str1 = "test=\"-1\"";
should become
String str2 = "test=\"31\"";
but also work for
String str3 = "test=\"foobar\"";
basically I want to turn this
String str4 = "test=\"antyhingCanGoHere\"";
into this
String str4 = "test=\"31\"";
Have tried:
Case insensitive Regex without using RegexOptions enumeration
How do you do case-insensitive string replacement using regular expressions?
Replace any character in between AnyText: and <usernameredacted#example.com> with an empty string using Regex?
Replace string in between occurrences
Replace a String between two Strings
Current code:
Regex RemoveName = new Regex("(?VARIABLE=\").*(?=\")", RegexOptions.IgnoreCase);
String convertSeccons = RemoveName.Replace(ruleFixed, "31");
Returns error:
System.ArgumentException was caught
Message=parsing "(?VARIABLE=").*(?=")" - Unrecognized grouping construct.
Source=System
StackTrace:
at System.Text.RegularExpressions.RegexParser.ScanGroupOpen()
at System.Text.RegularExpressions.RegexParser.ScanRegex()
at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, Boolean useCache)
at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options)
at application.application.insertGroupID(String rule) in C:\Users\winserv8\Documents\Visual Studio 2010\Projects\application\application\MainFormLauncher.cs:line 298
at application.application.xmlqueryDB(String xmlSaveLocation, TextWriter tw, String ruleName) in C:\Users\winserv8\Documents\Visual Studio 2010\Projects\application\application\MainFormLauncher.cs:line 250
InnerException:
found answer
string s = Regex.Replace(ruleFixed, "VARIABLE=\"(.*)\"", "VARIABLE=\"31\"");
ruleFixed = s;
I found this code sample at Replace any character in between AnyText: and with an empty string using Regex? which is one of the links i previously posted and just had skipped over this syntax because i thought it wouldnt handle what i needed.
var str1 = "test=\"foobar\"";
var str2 = str1.Substring(0, str1.IndexOf("\"") + 1) + "31\"";
If needed add check for IndexOf != -1
I don't know if I understood you correct, but if you want to replace all chars inside string, why aren't you using simple regular expresission
String str = "test=\"-\"1\"";
Regex regExpr = new Regex("\".*\"", RegexOptions.IgnoreCase);
String result = regExpr.Replace(str , "\"31\"");
Console.WriteLine(result);
prints:
test="31"
Note: You can take advantage of plain old XAttribute
String ruleFixed = "test=\"-\"1\"";
var splited = ruleFixed.Split('=');
var attribute = new XAttribute(splited[0], splited[1]);
attribute.Value = "31";
Console.WriteLine(attribute);//prints test="31"
var parts = given.Split('=');
return string.Format("{0}=\"{1}\"", parts[0], replacement);
In the case that your string has other things in it besides just the key/value pair of key="value", then you need to make the value-match part not match quote marks, or it will match all the way from the first value to the last quote mark in the string.
If that is true, then try this:
Regex.Replace(ruleFixed, "(?<=VARIABLE\s*=\s*\")[^\"]*(?=\")", "31");
This uses negative look-behind to match the VARIABLE=" part (with optional white space around it so VARIABLE = " would work as well, and negative look-ahead to match the ending ", without including the look-ahead/behind in the final match, enabling you to just replace the value you want.
If not, then your solution will work, but is not optimal because you have to repeat the value and the quote marks in the replace text.
Assuming that the string within the quotes does not contain quotes itself, you can use this general pattern in order to find a position between a prefix and a suffix:
(?<=prefix)find(?=suffix)
In your case
(?<=\w+=").*?(?=")
Here we are using the prefix \w+=" where \w+ denotes word characters (the variable) and =" are the equal sign and the quote.
We want to find anything .*? until we encounter the next quote.
The suffix is simply the quote ".
string result = Regex.Replace(input, "(?<=\\w+=\").*?(?=\")", replacement);
Try this:
[^"\r\n]*(?:""[\r\n]*)*
var pattern = "\"(.*)?\"";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
var replacement = regex.Replace("test=\"hereissomething\"", "\"31\"");
string s = Regex.Replace(ruleFixed, "VARIABLE=\"(.*)\"", "VARIABLE=\"31\"");
ruleFixed = s;
I found this code sample at Replace any character in between AnyText: and <usernameredacted#example.com> with an empty string using Regex? which is one of the links i previously posted and just had skipped over this syntax because i thought it wouldnt handle what i needed.
String str1 = "test=\"-1\"";
string[] parts = str1.Split(new[] {'"'}, 3);
string str2 = parts.Length == 3 ? string.Join(#"\", parts.First(), "31", parts.Last()) : str1;
String str1 = "test=\"-1\"";
string res = Regex.Replace(str1, "(^+\").+(\"+)", "$1" + "31" + "$2");
Im pretty bad at RegEx but you could make a simple ExtensionMethod using string functions to do this.
public static class StringExtensions
{
public static string ReplaceBetweenQuotes(this string str, string replacement)
{
if (str.Count(c => c.Equals('"')) == 2)
{
int start = str.IndexOf('"') + 1;
str = str.Replace(str.Substring(start, str.LastIndexOf('"') - start), replacement);
}
return str;
}
}
Usage:
String str3 = "test=\"foobar\"";
str3 = str3.ReplaceBetweenQuotes("31");
returns: "test=\"31\""

Categories