我无法让这个工作..
我有一个我希望在空格上分割的字符串。但是,我不想拆分字符串。也就是说,文本是双引号或单引号。
实施例
拆分以下字符串:
private String words = " Hello, today is nice " ;
..应生成以下令牌:
private
String
words
=
" Hello, today is nice "
;
我可以使用什么样的正则表达式?
答案 0 :(得分:0)
正则表达式([^ "]*)|("[^"]*")
应匹配所有令牌。借鉴我对Java和http://www.regular-expressions.info/java.html的有限知识,您应该能够做到这样的事情:
// Please excuse any syntax errors, I'm used to C#
Pattern pattern = Pattern.compile("([^ \"]*)|(\"[^\"]*\")");
Matcher matcher = pattern.matcher(theString);
while (matcher.find())
{
// do something with matcher.group();
}
答案 1 :(得分:0)
你试过这个吗?
((['"]).*?\2|\S+)
这是它的作用:
( <= Group everything
(['"]) <= Find a simple or double quote
.*? <= Capture everything after the quote (ungreedy)
\2 <= Find the simple or double quote (same as we had before)
| <= Or
\S+ <= Non space characters (one at least)
)
另外请注意,如果要创建解析器,请执行解析器并且不要使用正则表达式。