简体   繁体   中英

regular expression - parse classpath location

$JAR_REPO/nlb/grbox/smnt.jar

I want to get the string between $ and first / and this will be replaced with some other string.

  1. What is the regex to get JAR_REPO alone from above?
  2. Can I use Regex to get the actual string like the pattern match (any method) will return the string JAR_REPO?

Please help.

Thanks. Wells

\$([^/]+)/.*

or, as a Java String:

"\\$([^/]+)/.*"

The JAR_REPO String will be the group(1) :

Pattern pattern = Pattern.compile("\\$([^/]+)/.*");
Matcher matcher = pattern.matcher(yourstring);
if (matcher.find()) {
    String jarRepo = matcher.group(1);
}

这种类型的递归解析方法可以与解析方法一起使用解释器模式逻辑来解决。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM