inside
inside

Reputation: 3177

Ignore the first occurence of a character if it exists later

I am trying to understand how to implement "if then" with Regex in C#

Background: I am parsing a string that contains a token inside it, currently in order for token to be found user has to provide it with the curly brackets format e.g.

Some text {someToken} other text

With the following Regular Expression:

     [{](?<token>[^}]+)[}]"

I am able to get the token someToken. Now the new requirement that I got is that user should be allowed to input curly brackets as the token value, e.g.

Some text {{someToken}} other text

and the value that I will get is {someToken}. Is there such a thing in C# Regex as "If I have closing curly bracket, and there is another one after it, ignore the first one."?

Upvotes: 3

Views: 68

Answers (1)

anubhava
anubhava

Reputation: 784998

You can just use this greedy regex:

{(?<token>{?[^}]+}?)}

RegEx Demo

Upvotes: 5

Related Questions