Java regex (java.util.regex). Search for dollar sign

回眸只為那壹抹淺笑 提交于 2019-12-05 21:35:30

You may use

String search = "/bla/$V_N.$XYZ.bla";
String pattern = "[%$]([^%.$]*)";
Matcher matcher = Pattern.compile(pattern).matcher(search);
while (matcher.find()){
    System.out.println(matcher.group(1)); 
} // => V_N, XYZ

See the Java demo and the regex demo.

NOTE

  • You do not need an optional \1? at the end of the pattern. As it is optional, it does not restrict match context and is redundant (as the negated character class cannot already match neither $ nor%)
  • [%$]([^%.$]*) matches % or $, then captures into Group 1 any zero or more chars other than %, . and $. You only need Group 1 value, hence, matcher.group(1) is used.
  • In a character class, neither . nor $ are special, thus, they do not need escaping in [%.$] or [%$].
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!