简体   繁体   English

正则表达式-解析类路径位置

[英]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. 我想获取$和first /之间的字符串,并将其替换为其他字符串。

  1. What is the regex to get JAR_REPO alone from above? 从上面单独获得JAR_REPO的正则表达式是什么?
  2. Can I use Regex to get the actual string like the pattern match (any method) will return the string JAR_REPO? 我可以使用Regex来获取实际的字符串,例如模式匹配(任何方法)将返回字符串JAR_REPO吗?

Please help. 请帮忙。

Thanks. 谢谢。 Wells 韦尔斯

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

or, as a Java String: 或者,作为Java字符串:

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

The JAR_REPO String will be the group(1) : JAR_REPO字符串将为group(1)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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