2

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.

6
  • Why would you want to do this by regex? Just match [start]whatever[end], and manually remove the first 7 and the last 5 characters, leaving whatever. Commented Nov 26, 2009 at 10:49
  • @pp indeed, i just found out tx Commented Nov 26, 2009 at 10:55
  • I downvoted, Peter, because I answered your question. Then you said "it's a .NET issue" so I provided .NET code. Then you said "it's not a syntax issue" and I was downvoted. Sigh. You try and help people and they are just short and sharp and dismissive. Commented Nov 26, 2009 at 10:58
  • @PP that is misuse for downvote, it has nothing to do with the question. You hae downvoted me because you where downvoted and you can only downvote as reaction on the question, not as a kind of little revenge. Commented Nov 26, 2009 at 11:04
  • By the way the tag was indicating from the beginning it's a NET issue Commented Nov 26, 2009 at 11:08

4 Answers 4

5

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;
Sign up to request clarification or add additional context in comments.

7 Comments

sounds very usefull, but in a test, i still yields the start and the endtag for me
test was this btw : printfn "%s" (Regex.Match(@"[start]result[end]", @"(?:[start])(.*?)(:?[end])").Groups.[0].Value) which yields [start]result[end]
That is because you are looking at group 0 which is the whole string. Instead use group 1 which is just the bit in the parantheses.
In fact, I am being stupid — you could just use groups. I'll update my example.
(For posterity I initially suggested non-capturing groups (?:) but this is overkill for the specified problem.
|
3

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.

Comments

1

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

C#

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

VB

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

1 Comment

just removed the group capturing to 'start' and 'end'
0

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.

8 Comments

see the .NET tag as for the library
I know the syntax. tx not the issue however
If the syntax isn't the issue, then what is the issue? You pointed out you were using .NET which seems pretty syntax-specific.
I have flagged this answer for being called an idiot and lazy, (see edit remarks) This is not a direction i like SO to turn to
Agreed; completely inappropriate and unacceptable. @PP - I can't e-mail you privately since you don't have one listed - but note: that type of behaviour will not be tolerated; please keep it civil, or we will be forced to suspend your account.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.