Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to match {asdf} but not {{asdf}}

Tags:

regex

vbscript

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.

like image 818
GWR Avatar asked Dec 09 '12 14:12

GWR


People also ask

What does the regular expression '[ a za z ]' match?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.

What is the regex for string?

A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern.

How to use regular expression in Eclipse search?

Eclipse supports regular expressions within the dialog 'Find/Replace' (CTRL+F) and from Search->Search(CTRL+H). Don't forget to check the Regular Expression check box. NOTE: while finding the regex pattern you must not forget to escape the non-printing characters, symbols etc. The backslash \ is an escape character.


2 Answers

If your regex engine supports lookarounds, they are the way to go:

(?<!\{)\{\w+\}(?!\})

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:

(?<![{])[{]\w+[}](?![}])

EDIT:

It seems like VBScript does not support lookbehinds.

That is a bit of an issue. The closest thing you can get is:

(^|[^{])[{]\w+[}](?![}])

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 \G as 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):

(^|[^{])[{]\w+(?=[}](?![}]))

Now you will get matches {some and }{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 use

(^|[^{])([{]\w+)(?=[}](?![}]))

Then retrieve capturing group 2 and append }.

like image 70
Martin Ender Avatar answered Oct 06 '22 01:10

Martin Ender


This expression works perfectly:

^{{1}(\w+?)}{1}$ 
like image 21
ISORTEGAH Avatar answered Oct 06 '22 01:10

ISORTEGAH