Ajoutez l'un des éléments suivants avant de demander des groupes.
matcher.find();
matcher.maches();
Comment cela fonctionne:
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.
Source: Java Api
Personnellement, je recommande de supprimer plusieurs espaces, puis diviser et couper - simple, alto, testé et œuvres.
Essayez ceci:
String s = "William Faulkner - 'Light In August'";
String o[] = s.replaceAll("\\s+", " ").split("-");
String author = o[0].trim();
String bookTitle = o[1].trim();
Si vous:
System.out.println(author);
System.out.println(bookTitle);
Ensuite la sortie serait:
William Faulkner
'Light In August'
Juste testé votre regex sur mon ordinateur et cela fonctionne pour moi –