MasterOfVDL
MasterOfVDL

Reputation: 130

Capturing text between two words using a regular expression

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.

Upvotes: 4

Views: 9019

Answers (1)

Cylian
Cylian

Reputation: 11181

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.

Upvotes: 9

Related Questions