I want to extract only those words within double quotes. So, if the content is:
Would "you" like to have responses to your "questions" sent to you via email?
The answer must be
The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.
C++11 now finally does have a standard regex library - std::regex.
$ means "Match the end of the string" (the position after the last character in the string).
Pattern matching in C− We have to find if a string is present in another string, as an example, the string "algorithm” is present within the string "naive algorithm". If it is found, then its location (i.e. position it is present at) is displayed.
Try this regex
:
\"[^\"]*\"
or
\".*?\"
explain :
[^ character_group ]
Negation: Matches any single character that is not in character_group.
*?
Matches the previous element zero or more times, but as few times as possible.
and a sample code:
foreach(Match match in Regex.Matches(inputString, "\"([^\"]*)\"")) Console.WriteLine(match.ToString()); //or in LINQ var result = from Match match in Regex.Matches(line, "\"([^\"]*)\"") select match.ToString();
Based on @Ria 's answer:
static void Main(string[] args) { string str = "Would \"you\" like to have responses to your \"questions\" sent to you via email?"; var reg = new Regex("\".*?\""); var matches = reg.Matches(str); foreach (var item in matches) { Console.WriteLine(item.ToString()); } }
The output is:
"you" "questions"
You can use string.TrimStart() and string.TrimEnd() to remove double quotes if you don't want it.
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