I have a very straightforward problem.
I am using this regular expression to match instances of {somestring}.
\{{1}(\w+?)\}{1}
The problem is that I need it to ignore instances of {{somestring}}, but of course, it is matching the inner {somestring} in {{somestring}}.
Any idea how I can tweak the expression to skip anything like {{somestring}}?
I am using vbscript’s regular expression engine.
If your regex engine supports lookarounds, they are the way to go:
This does literally what you want. The lookbehind asserts that there is no
{preceding your{, and the lookahead asserts that there is no}following your}.Note that
{1}does never do anything. Ever.Also note that you don’t need to make
\w+ungreedy, because it cannot consume}anyway.Finally, I just want to put it here, that an alternative to escaping
{and}is to put it into a one-character character class. It’s a matter of taste which one you prefer but I like the readability of this one better:EDIT:
It seems like VBScript does not support lookbehinds.
That is a bit of an issue. The closest thing you can get is:
However, if the match is not found at the beginning of the string, this will include the preceding character in the match. This alone is not a problem, because you could get rid of that character through capturing of substring functions or something. However, matches cannot overlap, so if you have an input like
{some}{string}you won’t easily get both matches (because the first}has to be part of the second match). Some engines provide\Gas the equivalent of^for continuing matches, but VBScript does not seem to support that either. Hence, it’s going to get ugly from here on.What you could do is to exclude the closing
}from the match (using another lookahead):Now you will get matches
{someand}{string, so will have to append every match with}and remove the first character from every match that is not at the beginning of your string. Or if you can get hold of the captured results, you can useThen retrieve capturing group
2and append}.