I got this code below that works for single quotes. it finds all the words between the single quotes. but how would I modify the regex to work with double quotes?
keywords is coming from a form post
so
keywords = 'peace "this world" would be "and then" some' // Match all quoted fields MatchCollection col = Regex.Matches(keywords, @"'(.*?)'"); // Copy groups to a string[] array string[] fields = new string[col.Count]; for (int i = 0; i < fields.Length; i++) { fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group) }// Match all quoted fields MatchCollection col = Regex.Matches(keywords, @"'(.*?)'"); // Copy groups to a string[] array string[] fields = new string[col.Count]; for (int i = 0; i < fields.Length; i++) { fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group) }
There is no built-in support for regex in ANSI C.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan) Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options and time-out interval.
You would simply replace the '
with \"
and remove the literal to reconstruct it properly.
MatchCollection col = Regex.Matches(keywords, "\\\"(.*?)\\\"");
The exact same, but with double quotes in place of single quotes. Double quotes aren't special in a regex pattern. But I usually add something to make sure I'm not spanning accross multiple quoted strings in a single match, and to accomodate double-double quote escapes:
string pattern = @"""([^""]|"""")*"""; // or (same thing): string pattern = "\"(^\"|\"\")*\"";
Which translates to the literal string
"(^"|"")*"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With