Ath*_*han 2 java regex pattern-matching
我有一个正则表达式,[\\.|\\;|\\?|\\!][\\s]
这是用于分割字符串.但. ; ? !如果它在引号中,我不希望它分裂.
我不会使用split而是Pattern&Matcher.
演示:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String text = "start. \"in quotes!\"; foo? \"more \\\" words\"; bar";
String simpleToken = "[^.;?!\\s\"]+";
String quotedToken =
"(?x) # enable inline comments and ignore white spaces in the regex \n" +
"\" # match a double quote \n" +
"( # open group 1 \n" +
" \\\\. # match a backslash followed by any char (other than line breaks) \n" +
" | # OR \n" +
" [^\\\\\r\n\"] # any character other than a backslash, line breaks or double quote \n" +
") # close group 1 \n" +
"* # repeat group 1 zero or more times \n" +
"\" # match a double quote \n";
String regex = quotedToken + "|" + simpleToken;
Matcher m = Pattern.compile(regex).matcher(text);
while(m.find()) {
System.out.println("> " + m.group());
}
}
}
Run Code Online (Sandbox Code Playgroud)
产生:
> start
> "in quotes!"
> foo
> "more \" words"
> bar
Run Code Online (Sandbox Code Playgroud)
如您所见,它还可以处理引用标记内的转义引号.
| 归档时间: |
|
| 查看次数: |
5991 次 |
| 最近记录: |