0

I want to find a pattern """("\"\{.*?\}\"")""" in a string and drop "{ and }" from the start and the end respectively from all instances of the pattern found in the string.

For example: "batters": "{{"id":"1001"}}" should be replaced by "batters": {"id":"1001"}

Is there any Regex function that can help me?

3
  • Something like """\"batters\": \"{{\"id\":\"1001\"}}\"""".replaceAll("""\"{(.*?)}\"""", "$1")? Commented Jul 28, 2015 at 15:16
  • Hey, @stribizhev, you may forget to escape { in """\"{(.*?)}\"""", java.util.regex.PatternSyntaxException is thrown when running the code. Commented Jul 28, 2015 at 15:55
  • Whatever. You answered. Commented Jul 28, 2015 at 16:47

1 Answer 1

1

Happen to see a detailed explanation about this usage of regular expression in JavaScript: The Definitive Guide 6th Edition 10.2 String Methods for Pattern Matching

Recall that parenthesized subexpressions of a regular expression are numbered from left to right and that the regular expression remembers the text that each subexpression matches. If a $ followed by a digit appears in the replacement string, replace() replaces those two characters with the text that matches the specified subexpression

scala> """"{{"id":"1001"}}"""".replaceAll("""\"\{(.*?)\}\"""", "$1")
res15: String = {"id":"1001"}

The code above should solve you problem.

Sign up to request clarification or add additional context in comments.

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.