简体   繁体   English

使用正则表达式从字符串中提取子字符串

[英]Extract a sub string from a string using regular expression

Given the following Java codes: 给定以下Java代码:

String test = "'abc,ab,123',,,'123,aa,abc',,,";
Pattern p = Pattern.compile("('\\w\\S+\')");
Matcher m = p.matcher(test);
boolean s = m.matches();
System.out.println(s);

I want to extract all the content in '' , for example I want 'abc,ab,123' and '123,aa,abc' . 我想提取''所有内容,例如我想要'abc,ab,123''123,aa,abc' Why the outout is: 为什么出局是:

false

My regular expression is like: "find a ' ,followed by a number or a letter, followed by several non-space characters,followed by another ' ". 我的正则表达式是这样的:“找到一个' ,后跟一个数字或字母,后跟几个非空格字符,后跟另一个' ”。 It should have a match, what's wrong? 应该有一场比赛,怎么了?

Matcher.matches will try to check if regex can match entire string (see the documentation here ). Matcher.matches将尝试检查正则表达式是否可以匹配整个字符串(请参见此处的文档)。 Since your regex expects ' at the end, but your string last character is , matches returns false. 由于您的正则表达式期望在末尾加上' ,但字符串的最后一个字符为,匹配返回false。
If you want to print one or more part of string which matches your regex you need to use Matcher.find method first to let regex engine localize this matching substring. 如果要打印与正则表达式匹配的字符串的一个或多个部分,则需要首先使用Matcher.find方法,以使正则表达式引擎将该匹配的子字符串本地化。 To get next matching substring you can call find again from the same Matcher. 要获取下一个匹配的子字符串,您可以从同一Matcher中再次调用find To get all matching substrings call find until it returns false as response. 要获取所有匹配的子字符串,请调用find直到它返回false作为响应。

Try: 尝试:

String test = "'abc,ab,123',,,'123,aa,abc',,,";
Pattern p = Pattern.compile("'[^']*'");//extract non overlapping string between single quotes
Matcher m = p.matcher(test);
while (m.find()) { //does the pattern exists in input (sub)string
  System.out.println(m.group());//print string that matches pattern
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM