Peter
Peter

Reputation: 48958

How to find a string between two other strings with regex?

More or less obvious is :

\[start\](.*?)\[end\]

but that yields the [start] and [end] tag too. How do you omit them?

E.g.: f("[somestartstring]result[someendstring]") == "result"

UPDATE: the suggested answers are not working. My code is:

printfn "%s" (Regex.Match(@"[start]result[end]",
                          "\\[start\\](.*?)\\[end\\]").Groups.[0].Value)

but it still yields the surrounding start and end tags.

My mistake is: the 0 index! Thank you.

Upvotes: 2

Views: 14150

Answers (4)

PP.
PP.

Reputation: 10864

Depends on the language. Usually you have to specify the matching group you want returned; often group zero is the whole matching expression, 1 is the first matching group, 2 is the second matching group, and so forth.

Update 1: please see http://www.regular-expressions.info/dotnet.html

Update 2: Author seemed to think he understood .NET syntax. So removing code example and letting answer stand on its own.

Upvotes: 0

Paul Ruane
Paul Ruane

Reputation: 38580

You need to use a group, which is a match string within parantheses:

\[start\](.*?)\[end\]

These are numbered from 1 when you come to read them (zero being the whole matched string). (There is also the facility of named groups if you find that more intuitive.)

E.g. in C#:

Match match = new Regex("\[start\](.*?)\[end\]").Match("[start]blah[end]");
string value = match.Groups[1].Value;

Upvotes: 5

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

Remember Groups[0] matches the entire input. If you just want the first captured group it is Groups[1], so

string text = "[start]blahblah[end]";
Console.WriteLine(Regex.Match(text, @"\[start\](.*?)\[end\]").Groups[1].Value);

prints blahblah.

Upvotes: 3

Sandeep Kumar M
Sandeep Kumar M

Reputation: 3851

Use \[start\](.*?)\[end\]

C#

Regex regex = new Regex("\\[start\\](.*?)\\[end\\]");

VB

Dim regex As Regex = New Regex("\[start\](.*?)\[end\]")

Upvotes: 1

Related Questions