If you have this character, discard the capture

Asked

Viewed 40 times

0

How do I stop regular expression from capturing what it has \} in the text? I want you to get all the }, but I want if it’s escaped with the character to be discarded, not ignored.

I’m trying that expression: [^\\]} but it returns a character before the }...

Summary: If you have the character \ before the }, disposal the capture.

  • I don’t know on . Net, but on pcre, this works: {(.*[^\\])}

  • This one is capturing the field {...} whole... I just want you to capture the } other than }...

  • 3

    Got it wrong. This expression works on Pcre (if I’ve got it right this time...): (?<!\\)(})

  • Thanks @Viníciusgobboa.deOliveira, it worked!

1 answer

2


In the comment of @Viníciusgobboa.deOliveira was used a Egative lookbehind assertion (?<! subexpression) to check the previous character. PCRE and . NET syntax is the same.

(?<!\\)(\})

It means: capture a square bracket only if there is no backslash before.

Important, the groups started with (? are not counted, then the group (\}) will have index 1.

  • 1

    Yeah, I didn’t want them to } were captured too, but then I put the not capturing group, in the way (?<!\\)(?:\}) and it worked, after that } didn’t appear on the mailing lists, and I was able to do the Regex.Split() normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.