0


I need to use the method replaceAll of the String class to extract a configuration path from a large String. I managed to exclude the left side of the String with a regular expression, however I'm facing problems with the right side. Here is it:

/home/user/qa/jboss-eap-7.2/standalone/configuration", "jboss.server.data.dir" : "/home/user/qa/jboss-eap-7.2/standalone/data", "jboss.server.deploy.dir" : "/home/user/qa/jboss-eap-7.2/standalone/data/content", "jboss.server.log.dir" : "/home/user/qa/jboss-eap-7.2/standalone/log", "jboss.server.name" : "fedora", "jboss.server.persist.config" : "true", "jboss.server.temp.dir" : "/home/user/qa/jboss-eap-7.2/standalone/tmp", "line.separator" : "\n", "logging.configuration" : "file:/home/user/qa/jboss-eap-7.2/standalone/configuration/logging.properties", "module.path" : "/home/user/qa/jboss-eap-7.2/modules", "org.apache.xml.security.ignoreLineBreaks" : "true", "org.jboss.boot.log.file" : "/home/user/qa/jboss-eap-7.2/standalone/log/server.log", "org.jboss.resolver.warning" : "true",

So I just want to capture the String /home/user/qa/jboss-eap-7.2/standalone/configuration and I have come up with this:

// String a contains the above text
String b = a.replaceAll("^\".*","");
System.out.println(b);

This is supposed to remove everything, beginning from the first occurrence of the quotes("). However it does not work as expected as the full String is returned. Anyone can help me to find out why? Thanks

Carla
  • 2,132
  • 2
  • 24
  • 38
  • 1
    You don't need `^` start of string anchor here. try `".*"` – Code Maniac Feb 13 '20 at 09:25
  • Thanks a lot. It worked! That leaves me a bit puzzled why the "^" character caused the expression to fail, but I will find it in the reg expr docs. – Carla Feb 13 '20 at 09:29
  • 1
    `^` means match must start from start of string, when you don't use `^` it will try to find first occurrence of `"` throughout the string and then proceed further with remaining pattern. – Code Maniac Feb 13 '20 at 09:31
  • Does this answer your question? [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Code Maniac Feb 13 '20 at 09:32
  • Cutting off the start and the beginning of the string with two different regular expressions seems a bit inefficient to me. Take a look at [Regex groups](https://www.tutorialspoint.com/javaregex/javaregex_capturing_groups.htm) and the Pattern / Matcher classes in the Java documentation, that way you can extract your target string without having to fire up a RegEx test twice. – Nightara Feb 13 '20 at 09:57

0 Answers0