0

I really can't figure this one out.

I have a string where I'm trying to match the three groups marked with opening and closing braces (e.g {content}), with the custom string I have below a double brace means it is escaped.

SetValue:{1} when {OVERVIEW{{}}.Debug=1} else {0}
// Here is an actual real life example
ValidWhen: {! Matches('^\\d{{3}}\\s\\d{{3}}\\s\\d{{3}}[\\s\\S]', COLLECTION.AccountNumber)} {Account Number must not be more than 9 numbers.}

I can't figure out a regex to match the second group as {OVERVIEW{{}}.Debug=1} instead of {OVERVIEW{{ the closest I've got is using this regex as I thought I might be able to use the lookbehind to prevent it stopping on the double braces.

/{(?!\}).*?\}/

Is this even possible? Or do I need some more elaborate string parsing?

6
  • @MarcoBonelli: Not an exact duplicate. This one doesn't have nested braces. Commented Oct 13, 2014 at 9:46
  • What this \\d{{3}} regex means? Commented Oct 13, 2014 at 9:46
  • 1
    @nhahtdh your regex won't work if there is anything inside {{}} .. Commented Oct 13, 2014 at 9:47
  • @AvinashRaj: I have state my assumption that the regex only works for "valid input", though I do make an assumption that {{}} never appear outside {}. Commented Oct 13, 2014 at 9:48
  • @AvinashRaj It's to do with the product it's used on, gets injected as a string, hence the double slash escape and the double braces indicate to escape a brace. Commented Oct 13, 2014 at 9:48

2 Answers 2

3

The following regex works for valid input1:

/{(?:[^{}]|{{|}})*}/

We simply disallow any {} inside braces, and allow only double {{ and }}.

1 Here are some input that the regex above may return unexpected result:

{{0}} text

This regex will match {0}, which is most likely incorrect.

{{ OVER}

Unbalaced braces and most likely invalid code.

{ A {B } C}

Nested braces - not sure if this is valid, but the regex above will grab {B }.

Demo on regex101

Sign up to request clarification or add additional context in comments.

Comments

1
{[^}{]*}|{\S+}

Try this. See demo.

http://regex101.com/r/sK8oK9/6

2 Comments

Thanks but breaks down without the spaces e.g. matching SetValue:{1}when{OVERVIEW{{}}.Debug=1} else {0}
Appreciated but I think it still falls down, I've updated the question to add a more involved real example.

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.