在java/android中使用regexp提取序列后的所有字符
想象一下这些字符串:在java/android中使用regexp提取序列后的所有字符,java,regex,Java,Regex,想象一下这些字符串: String s = "firstpartie_FOO_lastpartieofthestring" 或 对于regexp,我想提取第二个“\uxp”后面的字符串 我试过这个: Pattern p = Pattern.compile("(?<=_[A-Z]*_)"); Matcher m = p.matcher("lllalal_FOOBBARR_lastpartieofthestringofthedead") Pattern p=Pattern.compile(
String s = "firstpartie_FOO_lastpartieofthestring"
或
对于regexp,我想提取第二个“\uxp”后面的字符串
我试过这个:
Pattern p = Pattern.compile("(?<=_[A-Z]*_)");
Matcher m = p.matcher("lllalal_FOOBBARR_lastpartieofthestringofthedead")
Pattern p=Pattern.compile((?试试这个,它会查找任何数量的字符,后跟一个\uu
,然后再查找另一个数量的字符,后跟\u
,并捕获其他所有内容:
*...*.(.*)
还有一个关于Regex101的示例:
试试这个,它会查找任何数量的字符,后跟一个
,然后再查找另一个数量的字符,后跟
,并捕获所有其他内容:
*...*.(.*)
还有一个关于Regex101的示例:
如果要修正方法,请使用捕获组捕获字符串的其余部分:
String s = "lllalal_FOOBBARR_lastpartieofthestringofthedead";
Pattern p = Pattern.compile("^[^_]*_[^_]*_(.*)", Pattern.DOTALL);
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1));
}
// => lastpartieofthestringofthedead
见
这里,^[^.]*.
匹配字符串的开头(^
),0+字符而不是.
([^.]*
),.
,再次匹配0+字符而不是.
和.
,然后将字符串的其余部分捕获到组1中(使用(.*)
)
否则,使用将其分为3部分:
String s = "lllalal_FOOBBARR_lastpartieofthestring_of_thedead";
String[] res = s.split("_", 3);
if (res.length > 2) {
System.out.println(res[2]);
}
// => lastpartieofthestring_of_thedead
请参见如果要修正方法,请使用捕获组捕获字符串的其余部分:
String s = "lllalal_FOOBBARR_lastpartieofthestringofthedead";
Pattern p = Pattern.compile("^[^_]*_[^_]*_(.*)", Pattern.DOTALL);
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1));
}
// => lastpartieofthestringofthedead
见
这里,^[^.]*.
匹配字符串的开头(^
),0+字符而不是.
([^.]*
),.
,再次匹配0+字符而不是.
和.
,然后将字符串的其余部分捕获到组1中(使用(.*)
)
否则,使用将其分为3部分:
String s = "lllalal_FOOBBARR_lastpartieofthestring_of_thedead";
String[] res = s.split("_", 3);
if (res.length > 2) {
System.out.println(res[2]);
}
// => lastpartieofthestring_of_thedead
请看我发现了这种方法:
Pattern p = Pattern.compile("_[A-Z]*_(.*)");
我在正则表达式研究方面取得了进展,您好;-)
解释:
…在一个下划线后,后跟任意数量(使用星号)的大写字符=>[A-Z]*,后跟另一个下划线=>[A-Z]*_
…,捕获所有字符=>(*)
在这里查找示例:我发现这样做:
Pattern p = Pattern.compile("_[A-Z]*_(.*)");
我在正则表达式研究方面取得了进展,您好;-)
解释:
…在一个下划线后,后跟任意数量(使用星号)的大写字符=>[A-Z]*,后跟另一个下划线=>[A-Z]*_
…,捕获所有字符=>(*)
在这里找到示例:只需使用\u
拆分它并获取第三个数组元素只需使用\u
拆分它并获取第三个数组元素