Created
October 14, 2024 12:42
-
-
Save karenpayneoregon/09f79bcb4cf68c8e6a7dfe96c11407bb to your computer and use it in GitHub Desktop.
Extract quoted text in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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