Finding character which is not in double quote using Regular Expression [duplicate] - c#

This question already has answers here:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I am new in writing regular expression and I have the following Scenario.
I have a string, like :
string line = "if (true){var data = string.Format(\"something {0} {1}.\", \"is\", \"wrong\");}";
now I need to write a regular expression that just pick the closing curly braces which are not in the double quote
so far I tried this:
"(^(\"[^\"]*\")(}))+"
^(\"[^\"]*\") : I want to Ignore any substring which is inside double quote, AND
(}) : I want to take }
+: for at least 1 occurrence.
But it seems I Did something wrong. Could any one please guide me to sort out where I did the wrong?
Thank you.

You just need these parts of your regex:
(?:\"[^\"]*\")|(})
Regex live here.

Related

I want to do a search in real time of objects with Regex in c# [duplicate]

This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.

Regex: match string with parathese with '|' operator [duplicate]

This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 1 year ago.
I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.
Individually,
'Number' can match the word number
'Number[(]s[)]' can match Number(s).
However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".
What I have tried:
1: Put \b boundary around the second string, doesn't work.
2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option
I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.
Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it.
Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form.
Also the pattern should be: Number\(s\)|Number
The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".

Is it possible to place a string variable inside a Regex? [duplicate]

This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.

How to get all possible Regex Matches [duplicate]

This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV

How to get the text not in the square bracket using regex c#? [duplicate]

This question already has answers here:
C# Regex Split - everything inside square brackets
(2 answers)
C# Use Regex to Remove everything inside brackets and the brackets themselves
(5 answers)
Closed 5 years ago.
My string looks like below.
I want to get Completed from below string.
"Completed[TranslationTest]"
I want any text which is not in square bracket.
Making some assumptions here, what type of chracters can be inside those brackets, but basically what you want is a something followed by positive lookahead group, like this: \w*(?=\[[\w\W]*\])

Categories