© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
12 replies
yourFriend

✅ Need help with regular expression pattern

I am trying to match everything between two given tags using regular expression. For example:
string s = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
string startTag = "<upcase>";
string endTag = "</upcase>";

// I want it to match:
// 1. <upcase>yellow submarine</upcase>
// 2. <upcase>anything</upcase>

// This pattern match nothing
string pattern = $@"(?'startTag'\s*{startTag})" + $@"(?'text'.*(?!{endTag}))" + $@"(?'endTag'{endTag})";

// This pattern match till last endTag instead for first endTag after the startTag:
// 1. <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase>
string pattern = $@"(?'startTag'\s*{startTag})" + $@"(?'text'.*)" + $@"(?'endTag'{endTag})";

MatchCollection matches = Regex.Matches(s, pattern);
string s = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
string startTag = "<upcase>";
string endTag = "</upcase>";

// I want it to match:
// 1. <upcase>yellow submarine</upcase>
// 2. <upcase>anything</upcase>

// This pattern match nothing
string pattern = $@"(?'startTag'\s*{startTag})" + $@"(?'text'.*(?!{endTag}))" + $@"(?'endTag'{endTag})";

// This pattern match till last endTag instead for first endTag after the startTag:
// 1. <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase>
string pattern = $@"(?'startTag'\s*{startTag})" + $@"(?'text'.*)" + $@"(?'endTag'{endTag})";

MatchCollection matches = Regex.Matches(s, pattern);
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Please help me with regex pattern
C#CC# / help
4y ago
Need help with MVVM
C#CC# / help
6mo ago
✅ Need Help With Boids
C#CC# / help
9mo ago