[英]How to get String after a certain character using pattern matching?
String tect = "A to B";
Pattern ptrn = Pattern.compile("\\b(A.*)\\b");
Matcher mtchr = ptrn.matcher(tr.text());
while(mtchr.find()) {
System.out.println( mtchr.group(1) );
}
I am getting output A to B
but I want to B
. 我得到输出A to B
但我想要to B
Please help me. 请帮我。
You can just place the A
outside of your capturing group. 您可以将A
放在捕获组之外。
String s = "A to B";
Pattern p = Pattern.compile("A *(.*)");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1)); // "to B"
}
You could also split the string. 你也可以拆分字符串。
String s = "A to B";
String[] parts = s.split("A *");
System.out.println(parts[1]); // "to B"
更改模式以使用A
的后视可能断言检查:
Pattern ptrn = Pattern.compile("(?<=A)(.*)");
你可以在一行中完成:
String afterA = str.replaceAll(".*?A *", ""),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.