Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created October 14, 2024 12:42
Show Gist options
  • Save karenpayneoregon/09f79bcb4cf68c8e6a7dfe96c11407bb to your computer and use it in GitHub Desktop.
Save karenpayneoregon/09f79bcb4cf68c8e6a7dfe96c11407bb to your computer and use it in GitHub Desktop.
Extract quoted text in a string
using System.Text.RegularExpressions;
namespace Your_namespace;
internal static partial class Extensions
{
public static List<string> StringsBetweenQuotes(this string sender)
{
var matches = QuotesRegex().Matches(sender);
var strings = new List<string>();
foreach (Match match in matches)
{
strings.Add(match.Groups[0].Value);
}
return strings;
}
[GeneratedRegex("([\"'])(?:(?=(\\\\?))\\2.)*?\\1")]
public static partial Regex QuotesRegex();
}
internal class Program
{
private static void Main(string[] args)
{
string sentence = "The \"quick brown fox jumps\" over the \"lazy dog\"";
var list = sentence.StringsBetweenQuotes();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment