I have following scenario:
File '/oname=$INSTDIR\workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' '${ADDEDSOURCE}workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' ; Added file
File '/oname=$INSTDIR\workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\CoolUtil.class' '${ADDEDSOURCE}workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\CoolUtil.class' ; Added file
I want to replace the '$' Sign in the name of *.class (could be other as well) with '$$'. I am able to get the filename, but just want the one group of Dollar sign.
Here is my example: https://regex101.com/r/Wa2cF6/2
In java I do want to just do :
public static void main(String[] args) {
String s = "File '/oname=$INSTDIR\\workspace\\jetty\\work\\webapp\\WEB-INF\\classes\\com\\sample\\service\\core\\util\\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' \t'${ADDEDSOURCE}workspace\\jetty\\work\\webapp\\WEB-INF\\classes\\com\\sample\\service\\core\\util\\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' \t; Added file";
s = s.replace("\\\\([a-zA-Z0-9_$]+)\\.\\w+'\\s", "$$");
System.out.println(s);
}
And expect a result of
File '/oname=$INSTDIR\workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\SomeModule$$SomeModulesData$$SomeModulesDataBuilder.class' '${ADDEDSOURCE}workspace\jetty\work\webapp\WEB-INF\classes\com\sample\service\core\util\SomeModule$$SomeModulesData$$SomeModulesDataBuilder.class' ; Added file
Thanks for your assistance.
So you want to replace the $
sign only in the class name? I would definitely go on a lower level using indexes and the StringBuilder
class:
private static final String testString = "File '/oname=$INSTDIR\\workspace\\jetty\\work\\webapp\\WEB-INF\\classes\\com\\sample\\service\\core\\util\\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' \t'${ADDEDSOURCE}workspace\\jetty\\work\\webapp\\WEB-INF\\classes\\com\\sample\\service\\core\\util\\SomeModule$SomeModulesData$SomeModulesDataBuilder.class' \t; Added file";
public static void main(String[] args) {
System.out.println(replaceDollars(testString));
}
private static final Pattern PATH = Pattern.compile("(\\'[\\S]*)(\\\\[\\w$]+\\.class\\')");
public static String replaceDollars(String string) {
Matcher m = PATH.matcher(string);
StringBuilder builder = new StringBuilder();
int end = 0;
while (m.find()) {
builder
.append(string, end, m.start())
.append(m.group(1))
.append(m.group(2).replace("$", "$$"));
end = m.end();
}
builder.append(string, end, string.length());
return builder.toString();
}
Using the following regex: (\\'[\\S]*)(\\\\[\\w$]+\\.class\\')
to find files in the input string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.