2013年10月11日星期五

Regex problem

String:
/ a_xxxx / b_xxxx / c_xxx I need to check out
xxx, where xxx can be any character , a, b, c the position may not be fixed , or no example:
/ a_111/b_222/c_333
/ a_111/b_222
/ b_222/a_111/c_333

Will this regex should be how to write
------ Solution --------------------------- -----------------
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]);
}

------ Solution ----------------- ---------------------------
	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));
}
}

------ Solution ------- -------------------------------------
private static void mytest6 () ; {
String str = "/ a_sfsdf/b_werwer/c_333";
Pattern p = Pattern.compile ("[abc] _ (. *?) (([^ /]) $ | /)");
Matcher m = p.matcher (str);
while (m.find ()) {
if (m.group (3) == null) {
System.out.println (m.group (1));
} else {
System.out.println (m.group (1) + m.group (3));
}

}
}
------ Solution ----------------------------------- ---------
top look under the third floor ,
should be no problem
------ For reference only ------------------------------- --------


not mean that. By matcher first find, and then group got , I wrote a little behind do not know how to write.


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"));

The main problems are two, one is a, b, c the relative position determination , and the other b, c may not .
If the string yes yes / a_111/b_222/c_333, I want to wait until 111,222,333 ;
If the string yes yes / a_111/c_333, I want to wait until 111,333 ;
If the string yes yes / b_222/a_111/c_333, I want to wait until 222,111,333;
------ For reference only ------------- --------------------------
Why must a regular , first splist ("/"), in the split ("_" ;) not Well
------ For reference only ---------------------------------- -----
this good point

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));
}
}

------ For reference only ----------------------------------- ----
Thank you very much , just needs changed, the string should look like this:
/ a/111/b/222/c/333
Here 111,222,333 may be alphanumeric ;
Further a, b, c may be text that a combination of number of letters , but it is fixed.

------ For reference only ---------------------------------- -----
string might be:
/ title/111/content/222
------ For reference only -------------------------- -------------
	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));
}
}

------ For reference only ------------------- --------------------
thanks for the ladies and gentlemen , I know how to write.


Pattern pattern = Pattern.compile("/(title|content)/([^/]+)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group(2));
}


very, very grateful .

没有评论:

发表评论