Java 如何使用模式匹配获取特定字符后的字符串?
我正在将输出Java 如何使用模式匹配获取特定字符后的字符串?,java,regex,Java,Regex,我正在将输出A转换为B,但我希望转换为B 请帮帮我。更改您的模式,以便对A使用查找后可能断言检查: 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) ); } Pattern ptrn=Pattern
A转换为B
,但我希望转换为B
请帮帮我。更改您的模式,以便对
A
使用查找后可能断言检查:
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) );
}
Pattern ptrn=Pattern.compile((?您可以在一行中完成:
Pattern ptrn = Pattern.compile("(?<=A)(.*)");
您可以将A
放置在您的捕获组之外
String afterA = str.replaceAll(".*?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"
}
那是一些可怕的东西。
String s = "A to B";
String[] parts = s.split("A *");
System.out.println(parts[1]); // "to B"