Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 <[email protected]> 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.

like image 514
toosweetnitemare Avatar asked Jan 08 '13 22:01

toosweetnitemare


2 Answers

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"
like image 141
Ilya Ivanov Avatar answered Oct 13 '22 08:10

Ilya Ivanov


var str1 = "test=\"foobar\"";
var str2 = str1.Substring(0, str1.IndexOf("\"") + 1) + "31\"";

If needed add check for IndexOf != -1

like image 40
Magnus Avatar answered Oct 13 '22 09:10

Magnus