Unable to extract string between double quotes using regex
This regex should work
"(.+?)"
Regex101 demo
It uses the concept of Group capture
Extract the values between the double quotes using regex
You may use a regex that will match quote, New
, some chars other than "
and :
, then :
, and then any chars but "
up to a "
:
var keys = Regex.Matches(emailBody, @"""New[^"":]+:[^""]+""", RegexOptions.Singleline)
.OfType<Match>()
.Select(m => m.Value)
.Distinct()
.ToArray();
See the regex demo
Pattern details:
"
- a literal double quoteNew
- a literal substring[^":]+
- 1 or more characters other than"
and:
(the[^...]
is a negated character class):
- a literal colon[^"]+
- 1 or more characters other than"
"
- a literal double quote
Regex to extract string between quotes
Use positive lookbehind operator:
GroupCollection ids = Regex.Match(nodeValue, "(?<=ID=\")[^\"]*").Groups;
You also used a capturing group (the parenthesis), this is why you get 2 results.
Get numbers between quotes and backslash with regex in C#
If you needed to match a digit sequence between two double quotes, you could use var resultfinal = Regex.Match(input, @"(?<="")[0-9]+(?="")")?.Value;
. However, your original string contains escaped quotation marks, so you need to extract a digit sequence in between two \"
substrings.
You can use
var input = "\\\"26201\\\",7\\0\\0";
var firstPattern = @"(?<=\\"")[0-9]+(?=\\"")";
var resultfinal = Regex.Match(input, firstPattern)?.Value;
Console.WriteLine($"final result: '{resultfinal}'");
// => final result: '26201'
See the C# demo. The pattern is (?<=\\")[0-9]+(?=\\")
, see its online demo. Details:
(?<=\\")
- a positive lookbehind that requires a\"
substring to occur immediately to the left of the current location[0-9]+
- one or more ASCII digits (note\d+
matches any one or more Unicode digits including Hindi, Persian etc. digits unless theRegexOptions.ECMAScript
option is used)(?=\\")
- a positive lookahead that requires a\"
substring to occur immediately to the right of the current location.
Regex - Extract Words within brackets and quotes if it starts with a keyword only
The solution offered by Wiktor is the most logical to use, but for sake of RegEx challenge see this Pattern \[(?!mykey)[^\[]+|([^\s\[=\"]+)(?=[^\"]*\"\])
, check group #1 Demo
\[ # "["
(?! # Negative Look-Ahead
mykey # "mykey"
) # End of Negative Look-Ahead
[^\[] # Character not in [\[] Character Class
+ # (one or more)(greedy)
| # OR
( # Capturing Group (1)
[^\s\[=\"] # Character not in [\s\[=\"] Character Class
+ # (one or more)(greedy)
) # End of Capturing Group (1)
(?= # Look-Ahead
[^\"] # Character not in [\"] Character Class
* # (zero or more)(greedy)
\" # """
\] # "]"
) # End of Look-Ahead
c# regex to capture everything between 2 double-quotes, escaped double-quote included
This regex
should solve your purpose -
str = Regex.Replace(str, @"(""[^""\\]*(?:\\.[^""\\]*)*"")|", "$1");
RegEx: Grabbing values between quotation marks
I've been using the following with great success:
(["'])(?:(?=(\\?))\2.)*?\1
It supports nested quotes as well.
For those who want a deeper explanation of how this works, here's an explanation from user ephemient:
([""'])
match a quote;((?=(\\?))\2.)
if backslash exists, gobble it, and whether or not that happens, match a character;*?
match many times (non-greedily, as to not eat the closing quote);\1
match the same quote that was use for opening.
How to use Regex to search only inside strings (between double quotations)
Hint: .*
means any number of any characters
With regex if you search \"class\"
it exactly matches "class"
.
To match "<anything>class<anything>"
add any number of any characters before and after class
:
\".*class.*\"
To match '<anything>class<anything>'
as well (such as javascript strings) use this:
["'](.*)(class)(.*)["']
Regular expressions: extract all words out of quotes
Try this expression:
(?:^|")([^"]*)(?:$|")
The groups matched by it will exclude the quotation marks, because they are enclosed in non-capturing parentheses (?:
and )
. Of course you need to escape the double-quotes for use in C# code.
If the target string starts and/or ends in a quoted value, this expression will match empty groups as well (for the initial and for the trailing quote).
Related Topics
Multi-Select Dropdown List in ASP.NET
How to Do .Net Binary Serialization of an Object When You Don't Have the Source Code of the Class
How to Use Webbrowser Control Documentcompleted Event in C#
Need to Perform Wildcard (*,, etc) Search on a String Using Regex
Self-Referencing Many-To-Many Recursive Relationship Code First Entity Framework
Convert from SQLdatareader to JSON
Why Do I Need a Memory Barrier
How Does Comparison Operator Works with Null Int
Using Variables Inside Strings
Does Lock() Guarantee Acquired in Order Requested
Unit Testing and Checking Private Variable Value
Regex: C# Extract Text Within Double Quotes
Enumerate Windows Like Alt-Tab Does
Limiting Double to 3 Decimal Places