1

We have a String as below.

\config\test\[name="sample"]\identifier["2"]\age["3"]

I need to remove the quotes surrounding the numbers. For example, the above string after replacement should look like below.

\config\test\[name="sample"]\identifier[2]\age[3]

Currently I'm trying with the regex as below

String.replaceAll("\"\\\\d\"", "");

This is replacing the numbers also. Please help to find out a regex for this.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
ganesh
  • 25
  • 4
  • 3
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Esteban P. Jul 10 '17 at 07:30
  • 1
    Please also pay attention to the big orange **How to Format** box to the right of the text area (when you're initially asking the question), entire toolbar full of formatting aids, **[?]** button giving formatting help, and post preview area. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Jul 10 '17 at 07:33
  • Possible duplicate of https://stackoverflow.com/questions/36267354/java-string-replaceall-with-back-reference – JSN Jul 10 '17 at 07:36

3 Answers3

1

You can use replaceAll with this regex \"(\d+)\" so you can replace the matching of \"(\d+)\" with the capturing group (\d+) :

String str = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
str = str.replaceAll("\"(\\d+)\"", "$1");
//----------------------^____^------^^

Output

\config\test\[name="sample"]\identifier[2]\age[3]

regex demo


Take a look about Capturing Groups

Graham
  • 7,431
  • 18
  • 59
  • 84
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

We can try doing a blanket replacement of the following pattern:

\["(\d+)"\]

And replacing it with this:

\[$1\]

Note that we specifically target quoted numbers only appearing in square brackets. This minimizes the risk of accidentally doing an unintended replacement.

Code:

String input = "\\config\\test\\[name=\"sample\"]\\identifier[\"2\"]\\age[\"3\"]";
input = input.replaceAll("\\[\"(\\d+)\"\\]", "[$1]");
System.out.println(input);

Output:

\config\test\[name="sample"]\identifier[2]\age[3]

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can use:

(?:"(?=\d)|(?<=\d)")  

and replace it with nothing == ( "" )

fast test:

echo '\config\test\[name="sample"]\identifier["2"]\age["3"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g' 

the output:

\config\test\[name="sample"]\identifier[2]\age[3] 

test2:

echo 'identifier["123"]\age["456"]' | perl -lpe 's/(?:"(?=\d)|(?<=\d)")//g' 

the output:

identifier[123]\age[456]

NOTE

if you have only a single double quote " it works fine; otherwise you should add quantifier + for both beginning and end "

test3:

echo '"""""1234234"""""' | perl -lpe 's/(?:"+(?=\d)|(?<=\d)"+)//g'

the output:

1234234
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44