62,614
社区成员
发帖
与我相关
我的任务
分享
private static void mytest6() {
String str = "/a_xxxx/c_xxx/b_2234";
Pattern p = Pattern.compile("[abc]_(.*?)($|/)");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
}
String str = "/a_111/b_222/c_333";
Pattern pattern = Pattern.compile("/a_(?<a>.+?)(/|$)");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.find());
System.out.println(matcher.group("a"));
主要问题有两个,一个是a、b、c相对的位置确定,另外是b、c可能没有。
如果字符串是是/a_111/b_222/c_333,我希望等到111、222、333;
如果字符串是是/a_111/c_333,我希望等到111、333;
如果字符串是是/b_222/a_111/c_333,我希望等到222、111、333; public static void main(String[] args) {
String str = "/a_111/b_222/c_333xsdsd";
Pattern pattern = Pattern.compile("[abc]_([^/]+)");
Matcher m = pattern.matcher(str);
while(m.find()) {
System.out.println(m.group(1));
}
}
String s = "/a_xxxx/b_xxxx/c_xxx";
Pattern p = Pattern.compile("\\d+");//定义规则
String str[] = p.split(s);//进行拆分
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
Pattern pattern = Pattern.compile("/(title|content)/([^/]+)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group(2));
}
非常非常感谢。 public static void main(String[] args) {
String str = "/title/111/content/222";
Pattern pattern = Pattern.compile("[a-z]+/([^/]+)");
Matcher m = pattern.matcher(str);
while(m.find()) {
System.out.println(m.group(1));
}
}