0

In java I am using regex \".*?\". I used this for replacing all the string with doublequote with a term String.

Ex:
INPUT: Functions.unescapeJson("test")
Result : Functions.unescapeJson("String") 

But now I wanted to exclude some string if they contains double quote. So, I am using / as the escape character. How to achieve this.

Ex:
INPUT: Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson("test"), "m2m:cin.con"),"payloads_ul.dataFrameOutput"),"[/"Dimming Value/"]")

RESULT: Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(String), String),String),String)

But the result I am getting if I use the previous regex is:

Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(input.mIntegerm/:sgn.nev.rep), String),String),StringDimming ValueString)

How to achieve this using regex if it finds / it should neglect without replacing original string.

The code that I am using

public static void main(String[] args) {
    String STRINGVALIDATIONREGEX = "\".*?\"";
    String formula = "Functions.getJsonPath(Functions.getJsonPath(Functions.getJsonPath(Functions.unescapeJson(input.m2m/:sgn.nev.rep), \"m2m:cin.con\"),\"payloads_ul.dataFrameOutput\"),\"[\"Dimming Value\"]\")";
    System.out.println(formula.replace(STRINGVALIDATIONREGEX, "String"));
}

3 Answers 3

1

You can use this regex:

\"(\/?.)*?\"
Sign up to request clarification or add additional context in comments.

Comments

1

Use [^/] to match anything that is not a slash.

For example, [^/]?\".*[^/]?\" would catch quotes not preceded by /

Comments

1
"((?:[^"]|(?<=\/)")*)"
  1. " match a "
  2. [^"] match a non-quote character
  3. | or
  4. (?<=\/)") a quote character that is preceded by a /
  5. * match sub-expressions 2 - 4 zero or more times.
  6. " match a "

See Regex demo

If you believe that a string such as "abc/" is invalid, then you should use the stricter regex:

"((?:[^"\/]|\/")*)"
  1. " match "
  2. [^"\/] match a any character that isn't a quote for /
  3. | or
  4. \/" match a /" combination
  5. * match sub-expressions 2 - 4 zero or more times.
  6. " match a "

See Regex demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.