简体   繁体   English

需要一个正则表达式来使用Java提取字符串的一部分

[英]Need a regex to extract a portion of the string using java

I need a regex to extract the a portion of a string after 'n' occurrences of a particular character 我需要一个正则表达式来提取出现“ n”个特定字符后的字符串的一部分

For example the string is : "aa/bb/cc/dd/ee/ff/gg/hh/ii/jj/kk/ll/mm/nn/oo/pp" I need the content aa/bb/cc/dd/ee/ff 例如,字符串是:“ aa / bb / cc / dd / ee / ff / gg / hh / ii / jj / kk / ll / mm / nn / oo / pp”我需要内容aa / bb / cc / dd / EE / FF

The followed the below approach but couldn't get the match. 遵循以下方法,但无法获得匹配。 Please suggest i am wrong below 请在下面建议我错了

matcher = Pattern.compile("((?:[^/]*/){5})").matcher(regExString);
if (matcher.matches()) {
String str = matcher.group(1);
System.out.println(str);
}

try this 尝试这个

    if (matcher.find()) {
        String str = matcher.group(1);
        System.out.println(str);
    }

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

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