user2727704
user2727704

Reputation: 645

Hive - Remove substring from string

I need to replace substring from a given string with empty string with the substring appearing in different positions of the string.

I want to remove the "fruit":"apple" from these possible combinations of the strings and expected the corresponding string:

{"client":"web","fruit":"apple"}   --> {"client":"web"}
{"fruit":"apple","client":"web"}   --> {"client":"web"}
{"client":"web","fruit":"apple","version":"v1.0"} --> {"client":"web","version":"v1.0"}
{"fruit":"apple"}   -->  null or empty string

I used regexp_replace(str, "\,*\"fruit\"\:\"apple\"", "") but that didn't get me the expected results. What is the right way to construct the regex?

Upvotes: 0

Views: 299

Answers (1)

WeGa
WeGa

Reputation: 890

It seems that you are working with data in JSON format. Depending from included dependencies you can achieve it totally without regular expression.

For example, if you are using Google's lib Gson, then you can parse String to JsonObject and then remove property from it

String input = "your data";
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(input).getAsJsonObject();

try {
   String foundValue = o.getAsJsonPrimitive("fruit").getAsString();
   if ("apple".equals(foundValue)) {
      o.remove("fruit");
   }
} catch (Exception e) {
  e.printStackTrace();
}
String filteredData = o.toJSONString();

P.S. code is not final version, it might needs handling of some situations (when there is no such field, or it contains non-primitive value), need further details to cover it

P.P.S. IMO, using regex in such situatioins makes code less readable and flexible

Upvotes: 1

Related Questions