Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing text between two words using a regular expression

Tags:

c#

.net

regex

I am trying to get the text in between two keywords using a regular expression in CSharp. Although I already found a topic with the same heading, that one was about finding the text in between square brackets, which is rather easy, since you can use \[(?<blah>[^\]]+?)\] to do this.

What I am trying to do is finding the words Matched text 123./! in UnMatched text 123./!team. So my delimiters are Un and team. If I would build my RegEx the way I am used to, I would need to do three parts again: Un for the start delimiter at the beginning, team for the end delimiter at the end and a group (?<blah>...+?) which says "Anything but the string team". But I dunno how to express this in regular expressions.

Is there a way to say "not this string" instead of "not one of those characters"? Also since I don't know about differences between implementations of Regular Expressions: I am using System.Text.RegularExpressions.RegEx of the .NET-Framework to parse them, so of course the sample should be working with this implementations.

like image 931
MasterOfVDL Avatar asked Jan 17 '23 01:01

MasterOfVDL


1 Answers

You may use this syntax

(?s)(?<=start_delim).+?(?=end_delim)

just replace start_delim and end_delim as required. Visit here for more information in this regard.

like image 58
Cylian Avatar answered Jan 30 '23 13:01

Cylian