Cookie Policy
We may store information about you using cookies (files which are sent by us to your computer or other access device) which
we can access when you visit our site in future. We do this to enhance user experience. If you want to delete any cookies that
are already on your computer, please refer to the instructions for your file management software to locate the file or directory
that stores cookies. Our cookies will have the file names symfony, JSESSIONID, IS and ISCOOKIE. Information on deleting or controlling
cookies is available at www.AboutCookies.org. Please note that by
deleting our cookies or disabling future cookies you may not be able to access certain areas or features of our site.

Java RegEx mini tutotial replacing tricky characters

Recently I have been looking into DSL for expressing order promotion eligibilities. The backing engine for this was Groovy that allows to produce runtime scripts from user input. This makes very powerful since full potential of your application can be used in creating promotion conditions. However with this power comes the danger that users may misuse the language provided and mutate objects. Therefore a decision was made to replace all assignment (=) operators with equality (==) when preprocessing user input thus disallowing any updates to the objects in evaluation context.

So basically we needed to replace = with == in a multiline groovy script snippet.

In order to do this we need to consider the following cases:

line beginning with =

line ending with =

line that contains =

must not replace ==, >= and <= as these are valid read only comparison operators

Now we can do this by effectively writting handlers for each of those cases. However with java RegEx lookahead (?) and lookbehind (?

Although, I knew exactly what to do RegEx always have a knock-out effect on me. So let go through this mini tutorial to see exactly what is going on.

So we can rethink our requirement as:

We are looking for = character

It must not be preceeded by =, < or >

It must not be followed by =, < or >

The RegEx looks like this: "(?

= - middle part looking for = character which may be precedeed or followed by new line.

(? to make sure this is really an assignment operator and not ==, >= or <=

(?![=> to make sure this really an assignment operator and not ==, => or =

There are few more issues that we need to fix here:

We should also clean new lines inside the assignment as this can cause issue

=> and =< are not actually valid in terms or java operators, so there should be a replace call to make them >= and <=.

To overcome the first problem we need the following RegEx: (\s*)([=>

(\s*) - Any number of whitespace characters, short for [ \t\n\x0b\r\f] preceeding or following

([=> with at least one character, which account for all kinds of comparisons including assignment (e.g. =>, >, =, ==, === and so on). Note the + inside the brackets - we want to capture all characters.

Replacing => and =< does not need a RegEx since it can be done using a simple String.replace()

I hope this mini tutorial gave a bit more insight into what lookaround functions of java RegEx do. By no means above code is complete - there are few more improvements that can be done. The intention was to give you some insight into how to solve these kind of problems in java.