J'aurais fait (en Java) :
1 2 3 4 5 6 7 8 9 10 11
| Matcher matcher = Pattern.compile("(chapitre\\s+\\d+\\s?)(?:[^\"]*)(\"\\S+\")?").matcher(str);
while (matcher.find()) {
for(int i=1; i<=matcher.groupCount();i++) {
if ( matcher.group(i)!=null ) {
System.out.print(matcher.group(i));
}
}
System.out.println();
} |
Ce qui donne :
chapitre 1 "arthur"
chapitre 2 "nestor"
chapitre 3
Si on fait :
1 2 3 4 5 6
| Matcher matcher = Pattern.compile("(chapitre\\s+\\d+\\s?)(?:[^\"]*)(\"\\S+\")?").matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
} |
1 2 3 4 5 6 7 8 9 10 11
| chapitre 1
d
bcd
efg
toto participant "arthur"
chapitre 2
d
bcd
efg
toto participant "nestor"
chapitre 3 |
Et oui : on affiche tout ce qui matche (donc ce qui se trouve entre chapitre et participant (peu importe que le groupe soit capturant ou pas).
Maintenant si le (?=chapitre) que tu as mis à la fin de l'expression à pour but de ne pas sélectionner les chapitres sans auteur, on peut faire :
1 2 3 4 5 6 7 8 9 10 11 12 13
| Matcher matcher = Pattern.compile("(chapitre\\s+\\d+\\s)[\\s\\S]+?participant\\s+(\"\\S+\")")
.matcher(str);
while (matcher.find()) {
for(int i=1; i<=matcher.groupCount();i++) {
if ( matcher.group(i)!=null ) {
System.out.print(matcher.group(i));
}
}
System.out.println();
} |
Donnant :
chapitre 1 "arthur"
chapitre 2 "nestor"
Partager