The code below works
string pattern = "\"cs\":\".*?\"";
string replacement = "\"cs\":\"\"";
Regex rgx = new Regex(pattern);
string stripped = rgx.Replace(resp.JsonResponse, replacement);
But is it possible to tweak this so that I only replace .*? with nothing.
string pattern = "(exclude:\"cs\":\").*?(exclude:\")";
string replacement = "";
Regex rgx = new Regex(pattern);
string stripped = rgx.Replace(resp.JsonResponse, replacement);
Yes, by only matching .*? and adding positive lookahead/lookbehind assertions to ensure it only matches when the conditions are correct.
So, change your pattern to
(?<="cs":").*?(?=")
(unescaped)
Now, you'll only match .*? when it is preceded by "cs":" and followed by ".
Q: It looks like you're editing JSON. Is there a reason that you don't want to defer this work to a JSON parser?
Related
I want to replace strings like
Dependencies\myfolder\1\2\abc.dll
Dependencies\myfolder\abc.dll
Dependencies\myfolder\1\abc.dll
with
packages\abc.dll.
What is the suitable regex pattern to do this. I was expecting the pattern to be -
Dependencies*abc.dll
So my code is -
var newEntry = packages\abc.dll;
var pattern = Dependencies*abc.dll;
var allText = ""; //this contains the text read from a file
Regex rgx = new Regex(pattern);
rgx.Replace(allText, newEntry);
But this seems to be a wrong regex pattern.
Almost there you need the .* like:
Dependencies(.*)abc.dll
Online Demo
.NET Online Demo
I'm doing a regex that is trying to match the following string:
.\SQL2012
From the two strings (they are contained within another larger string but that is irrelevant in this case):
/SERVER "\".\SQL2012\""
/SERVER .\SQL2012
So the "\" before and the \"" after the match may both be omitted in some cases. The regex I've come up with (from a previous question here on StackOverflow) is the following:
(?<=\/SERVER\s*(?:[""\\""]+)?)\w+(?=(?:[\\""""]+|$)| )
Which works fine if I'm trying to match TEST_SERVER instead of .\SQL2012 (because \w does not match special characters). Is there a way to match anything until \"" or a whitespace occurs?
I'm doing this in C#, here's my code:
string input = "/SERVER \"\\\".\\SQL2012\\\"\"";
string pattern = #"(?<=\/SERVER\s*(?:[""\\""]+)?)\w+(?=(?:[\\""""]+|$)| )";
Regex regEx = new Regex(pattern);
MatchCollection matches = regEx.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine(match.ToString());
}
Console.ReadKey();
Add a word boundary \b just before to the lookahead,
string input = "/SERVER .\\SQL2012";
Regex rgx = new Regex(#"(?<=\/SERVER\s+""\\"").*?\b(?=\\""""|$| )|(?<=\/SERVER\s+).*?\b(?= |$)");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);
Console.WriteLine(input);
IDEONE
How can I find all the matches in a string using a regular expression run in C#?
I want to find all matches in the below example string.
Example:
inputString: Hello (mail) byebye (time) how are you (mail) how are you (time)
I want to match (mail) and (time) from the example. Including parentheses( and ).
In attempting to solve this, I've writtent the following code.
string testString = #"(mail)|(time)";
Regex regx = new Regex(Regex.Escape(testString), RegexOptions.IgnoreCase);
List<string> mactches = regx.Matches(inputString).OfType<Match>().Select(m => m.Value).Distinct().ToList();
foreach (string match in mactches)
{
//Do something
}
Is the pipe(|) used for the logical OR condition?
Using Regex.Escape(testString) is going to escape your pipe character, turning
#"(mail)|(time)"
effectively into
#"\(mail\)\|\(time\)".
Thus, your regex is looking for the literal "(mail)|(time)".
If all of your matches are as simple as words surrounded by parens, I would build the regex like this:
List<string> words = new List<string> { "(mail)", "(time)", ... };
string pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
Escape the parentheses in your test string:
string testString = #"\(mail\)|\(time\)";
Remove Regex.Escape:
Regex regx = new Regex(testString, RegexOptions.IgnoreCase);
Output (includes parentheses):
(mail)
(time)
The reason Regex.Escape isn't working in your case is that it escapes the | character as well:
Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and whitespace) by replacing them with their \ codes.
I have a string in the following format
ABC=23:Qasd=56:Def=40.44
I would like to replace all the strings (ABC=, Qasd= and Def=) with empty string. The string after = can be anything. So my output string would be
23:56:40.44
It would be great if you can let me know the regex for that in C#
(^|:)[^=]*=
replaced with
$1
Matches the beginning of a string or a : and everything until and including =.
It is replaced with $1 to keep :.
C#
string strTargetString = #"ABC=23:Qasd=56:Def=40.44";
var myRegex = new Regex(#"(^|:)[^=]*=");
var result = myRegex.Replace(strTargetString, #"$1");
//result: 23:56:40.44
More examples:
ABC=hello:Qasd=56:Def=40.44 => hello:56:40.44
Match
^[^=]+=|(?<=:)[^=]+=
and replace with string.Empty
Regex.Replace("ABC=23:Qasd=56:Def=40.44", #"^[^=]+=|(?<=:)[^=]+=", string.Empty);
I need to match all the whole words containing a given a string.
string s = "ABC.MYTESTING
XYZ.YOUTESTED
ANY.TESTING";
Regex r = new Regex("(?<TM>[!\..]*TEST.*)", ...);
MatchCollection mc = r.Matches(s);
I need the result to be:
MYTESTING
YOUTESTED
TESTING
But I get:
TESTING
TESTED
.TESTING
How do I achieve this with Regular expressions.
Edit: Extended sample string.
If you were looking for all words including 'TEST', you should use
#"(?<TM>\w*TEST\w*)"
\w includes word characters and is short for [A-Za-z0-9_]
Keep it simple: why not just try \w*TEST\w* as the match pattern.
I get the results you are expecting with the following:
string s = #"ABC.MYTESTING
XYZ.YOUTESTED
ANY.TESTING";
var m = Regex.Matches(s, #"(\w*TEST\w*)", RegexOptions.IgnoreCase);
Try using \b. It's the regex flag for a non-word delimiter. If you wanted to match both words you could use:
/\b[a-z]+\b/i
BTW, .net doesn't need the surrounding /, and the i is just a case-insensitive match flag.
.NET Alternative:
var re = new Regex(#"\b[a-z]+\b", RegexOptions.IgnoreCase);
Using Groups I think you can achieve it.
string s = #"ABC.TESTING
XYZ.TESTED";
Regex r = new Regex(#"(?<TM>[!\..]*(?<test>TEST.*))", RegexOptions.Multiline);
var mc= r.Matches(s);
foreach (Match match in mc)
{
Console.WriteLine(match.Groups["test"]);
}
Works exactly like you want.
BTW, your regular expression pattern should be a verbatim string ( #"")
Regex r = new Regex(#"(?<TM>[^.]*TEST.*)", RegexOptions.IgnoreCase);
First, as #manojlds said, you should use verbatim strings for regexes whenever possible. Otherwise you'll have to use two backslashes in most of your regex escape sequences, not just one (e.g. [!\\..]*).
Second, if you want to match anything but a dot, that part of the regex should be [^.]*. ^ is the metacharacter that inverts the character class, not !, and . has no special meaning in that context, so it doesn't need to be escaped. But you should probably use \w* instead, or even [A-Z]*, depending on what exactly you mean by "word". [!\..] matches ! or ..
Regex r = new Regex(#"(?<TM>[A-Z]*TEST[A-Z]*)", RegexOptions.IgnoreCase);
That way you don't need to bother with word boundaries, though they don't hurt:
Regex r = new Regex(#"(?<TM>\b[A-Z]*TEST[A-Z]*\b)", RegexOptions.IgnoreCase);
Finally, if you're always taking the whole match anyway, you don't need to use a capturing group:
Regex r = new Regex(#"\b[A-Z]*TEST[A-Z]*\b", RegexOptions.IgnoreCase);
The matched text will be available via Match's Value property.