rss
twitter
    Find out what I'm doing, Follow Me :)

Regex for Extracting quoted Words or Sentences

Here is another post extending the Regex patterns
Here i am sharing the regex issue i faced when finding a pattern for finding quoted words or sentences in a text content.

Code:

string str = "here i am 'presenting' pattern for \"Qouted\" words or \"Qouted Sentences\" in a pragragraph or text.";
Regex exp = new Regex("(['\"](?<A>[^'\"]*)['\"])");
foreach (Match myMatch in exp.Matches(str))
{
Console.WriteLine(myMatch.Groups["A"].Value);

}
Console.ReadLine();

OutPut:
presenting
Qouted
Qouted Sentences

what if i want to have all the words along with the qouted words and sentences.

Code:

string str = "here 'presenting' pattern for \"Qouted\" words or \"Qouted Sentences\" in pragragraph or text.";
Regex exp = new Regex("(?<A>[a-zA-z]+)|(['\"](?<A>[^'\"]*)['\"])");
foreach (Match myMatch in exp.Matches(str))
{
Console.WriteLine(myMatch.Groups["A"].Value);

}
Console.ReadLine();

OutPut:
here
presenting
pattern
for
Qouted
words
or
Qouted Sentences
in
pragragraph
or
text

Thanks for reading. will come soon with another post on C#

0 comments: