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 (\|).
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;
}
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); //=> "----"
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\""
Maybe simple question..
String text = "fake 43 60 fake";
String patt = "[43.60]";
Match m = Regex.Match(text, patt)
In this situation, m.Success = true because the dot replace any character (also the space). But I must match the string literally in the patt.
Of course, I can use the '\' before the dot in the patt
String patt = #"[43\.60]";
So the m.Success = false, but there's more special characters in the Regular Expression-world.
My question is, how can I use regular expression that a string will be literally taken as it set. So '43.60' must be match with exactly '43.60'. '43?60' must be match with '43?60'....
thanks.
To get a regex-safe literal:
string escaped = Regex.Escape(input);
For example, to match the literal [43.60]:
string escaped = Regex.Escape(#"[43.60]");
gives the string with content: \[43\.60].
You can then use this escaped content to create a regex; for example:
string find = "43?60";
string escaped = Regex.Escape(find);
bool match = Regex.IsMatch("abc 43?60", escaped);
Note that in many cases you will want to combine the escaped string with some other regex fragment to make a complete pattern.