在您提问之前添加以下内容之一。
matcher.find();
matcher.maches();
这是如何工作:
A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:
The matches method attempts to match the entire input sequence against the pattern.
The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
The find method scans the input sequence looking for the next subsequence that matches the pattern.
来源:Java Api
我个人建议你先删除多个空格,然后分裂和修剪 - 中提琴简单,测试和工程。
试试这个:
String s = "William Faulkner - 'Light In August'";
String o[] = s.replaceAll("\\s+", " ").split("-");
String author = o[0].trim();
String bookTitle = o[1].trim();
,如果您:
System.out.println(author);
System.out.println(bookTitle);
然后输出为:
William Faulkner
'Light In August'
刚刚在我的电脑上测试了你的正则表达式,它适用于我 – 2010-10-18 20:59:12