Est-ce que dans la vrai vie "chapitre" est toujours en début de ligne, et est-ce que chaque chapitre a toujours un participant?
Car dans ce cas, moi j'écrirai plutôt:
Matcher m = Pattern.compile("(?m)^chapitre\\s+(\\d+)\\W+(?>\\w+\\W+)*?participant\\s+\"([^\"]+)\".*\\n(?>.*\\n?)*?(?=^chapitre\\s|\\z)").matcher(str);
Au lieu de supporter un quantificateur non-gourmand pour chaque caractère avec [\\s\\S]*?, en utilisant (?>\\w+\\W+)*?, je saute de mot en mot, et avec (?>.*\\n?)*? de ligne en ligne, ce qui est déjà plus rapide (vue qu'on évite bon nombre de tests.)
À noter au passage qu'un word-boundary n'est pas nécessaire avant "participant" vu qu'il ne peut être précédé que par un caractère de la classe \W.
Partager