-1

We have a JAVA String as

String str = "WHERE geoAreaName=\"Barcelona (Spain) EUR\" AND (startDate=\"2019-01-01\" AND  endDate=\"2020-01-01\")";

We need to remove characters like [ , ], ( , ), { , } from it.

The regex pattern to identify the same is : [\\[\\](){}]

So on executing below code, the output is:

System.out.println(str.replaceAll("[\\[\\](){}]" , ""));

>>> WHERE geoAreaName="Barcelona Spain EUR" AND startDate="2019-01-01" AND endDate="2020-01-01"

This works fine, except we need to keep the data enclosed in double quotes intact.

Barcelona (Spain) EUR needs to be intact and not converted to Barcelona Spain EUR

The expected output is :

WHERE geoAreaName="Barcelona (Spain) EUR" AND startDate="2019-01-01" AND endDate="2020-01-01"

So in a nutshell, I need a regex which will identify the characters in given string except for the parts which are in quotes.

Any help is appreciated.

hemantvsn
  • 1,316
  • 3
  • 12
  • 24
  • Out of curiosity, why do you edit the SQL string in your java code with regex? Why do you want to remove the bracket characters? This sounds like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Progman Sep 28 '19 at 09:17
  • Actually I went with SQL example just to explain the problem better. This problem is in regex domain and thing Im looking for is `a regex which will identify and replace the characters in given string except for the parts which are in quotes.` – hemantvsn Oct 01 '19 at 02:29
  • Possible duplicate of [Can Regex be used for this particular string manipulation?](https://stackoverflow.com/questions/138552/can-regex-be-used-for-this-particular-string-manipulation) – Progman Oct 01 '19 at 16:31
  • You might also want to look at https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks – Progman Oct 01 '19 at 16:32
  • And maybe https://stackoverflow.com/questions/17431820/replace-part-of-string-between-quotes-in-php-regex as well – Progman Oct 01 '19 at 16:33

1 Answers1

0

I don't know why you want to remove the parentheses from your WHERE clause outside of string literals. They are superfluous, but there is nothing wrong with them being there. However, you have another problem, namely that you are using double quotes for string and date literals when you should be using single quotes. So I actually suggest the following replacement:

String where = "WHERE geoAreaName=\"Barcelona (Spain) EUR\" AND (startDate=\"2019-01-01\" AND  endDate=\"2020-01-01\")";
where = where.replaceAll("\"", "'");

The resulting WHERE clause is perfectly valid and should run as you expect:

WHERE
    geoAreaName = 'Barcelona (Spain) EUR' AND
    (startDate = '2019-01-01' AND  endDate = '2020-01-01')
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Actually I went with SQL example just to explain the problem better. This problem is in regex domain and thing Im looking for is `a regex which will identify and replace the characters in given string except for the parts which are in quotes.` Even if we go with your solution, the brackets are still there. – hemantvsn Oct 01 '19 at 02:32
  • @hemantvsn To handle your requirement in the general case you would need to write a SQL _parser_, not just a single regex. – Tim Biegeleisen Oct 01 '19 at 07:31