在您提問之前添加以下內容之一。
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