简体   繁体   English

如何使用模式匹配获取某个字符后的String?

[英]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.

相关问题 匹配在特定模式后出现的字符串 - Matching a string which occurs after a certain pattern 如何在与正则表达式模式不匹配的字符串中查找字符 - How to find a character in a string which is not matching the regex pattern 如何用匹配 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 长度的一个字符替换字符串模式? - How to replace string pattern by one character of length of matching substring length? 如何创建一种方法来检查字符串中的模式,并使用ArrayList返回模式后的字符? (提示中的提示) - How to create a method that checks for pattern in string and return the character after the pattern using an ArrayList? (Prompt in description) 如何使用 regex/java 在某个字符之后提取字符串的结尾? - How to extract the end of an string after a certain character with regex/java? 如何在不使用Java模式匹配的情况下匹配字符串中的多个文本? - How to match multiple texts in a String without using pattern matching in Java? 多字节字符-模式匹配 - Multibyte Character - Pattern matching 如何在正则表达式模式中包含某些字符? - How to include certain character in a regex pattern? 模式匹配:将字符串与模式匹配 - Pattern matching : Matching a String with a pattern 使用Java Pattern和Matcher,如何获取第一个匹配的标签内容 - Using java Pattern and Matcher, how to get first matching tag content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM