Reputation: 159
I've a RegEx and would like to ignore any ' (apostrophe) within the string. RegEx discussion can be found within the discussion String manipulation: How to replace a string with a specific pattern
RegEx: \\(\\s*'(?<text>[^'']*)'\\s*,\\s*(?<pname>[\\w\\[\\]]+)\\s*\\)
Basically the provided RegEx doesn't work in a scenario where the {text} contains ' (apostrophe). Can you please have the RegEx to ignore any apostroph'e within the {text}?
For eg:
substringof('B's',Name) should be replaced by Name.Contains("B's")
substringof('B'',Name) should be replaced by Name.Contains("B'")
substringof('''',Name) should be replaced by Name.Contains("'")
Appreciate it!! Thank you.
Upvotes: 0
Views: 1113
Reputation: 89557
It seems to be difficult to deal with the case ''''
. It's the reason why I choose to use delegate and an another replace to solve the issue.
static void Main(string[] args)
{
var subjects = new string[] {"substringof('xxxx',Name)", "substringof('B's',Name)", "substringof('B'',Name)", "substringof('''',Name)"};
Regex reg = new Regex(@"substringof\('(.+?)'\s*,\s*([\w\[\]]+)\)");
foreach (string subject in subjects) {
string result = reg.Replace(subject, delegate(Match m) { return m.Groups[2].Value + ".Contains(\"" + m.Groups[1].Value.Replace("''", "'") + "\")"; });
Console.WriteLine(result);
}
}
Upvotes: 1