/ / RegEx pour faire correspondre tous les caractères entre le dernier mot majuscule et un autre mot d'une chaîne - java, regex, regex-lookarounds

RegEx pour faire correspondre tous les caractères entre le dernier mot majuscule et un autre mot d'une chaîne - java, regex, regex-lookarounds

Je dois faire correspondre tous les caractères entre le dernier mot majuscule d'une chaîne et un autre mot. Texte de saisie:Le renard CLEVER a sauté sur le grand et le mur (trou 2) dans la nuit.

RegEx utilisé:

(?<=b[A-Z]+s)(.+?)(?=sin)

La regex ci-dessus donne fox JUMPED OVER the big and (Hole 2) wall

Production attendue: the big and (Hole 2) wall

Quelqu'un peut-il résoudre ce problème?

Réponses:

2 pour la réponse № 1

Ce n'est peut-être pas la solution la plus efficace, mais cela semble fonctionner:

String text = "The CLEVER fox JUMPED OVER the big wall in the night.";
String regex = "(\b[A-Z]+\s)(?!.*\b[A-Z]+\b)(.+?)(\sin)";
Matcher m = Pattern.compile(regex).matcher(text);
if (m.find()) {
System.out.println(m.group(2));
}

Il utilise une anticipation négative pour s'assurer qu'il n'y a plus de mots en majuscules dans le texte avant de capturer les données souhaitées.


1 pour la réponse № 2

Vous pouvez simplement exclure les majuscules dans votre seconde expression correspondante

(?<=b[A-Z]+s)([^A-Z]+)(?=sin)

Cela forcera la première partie à correspondre The CLEVER fox JUMPED OVER, la seconde expression correspondante donnera the big wall et le dernier correspond à la seule in séquence dans votre phrase test.


1 pour la réponse № 3

Que diriez-vous:

[A-Z][s.](?!.*?[A-Z])(.*)sin

Expl .: Trouvez une lettre majuscule suivie d'un espace, PAS suivie de rien suivi d'une lettre majuscule. Puis capturez tout ce qui peut aller jusqu’à, mais sans inclure, un espace suivi du mot donné.

Cela ne capture que la partie recherchée.

Cordialement


0 pour la réponse № 4

Que diriez-vous:

^.*(?:b[A-Z]+b)(.+?)(?=sin)

Explication:

The regular expression:

(?-imsx:^.*(?:b[A-Z]+b)(.+?)(?=sin))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^                        the beginning of the string
----------------------------------------------------------------------
.*                       any character except n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?:                      group, but do not capture:
----------------------------------------------------------------------
b                       the boundary between a word char (w)
and something that is not a word char
----------------------------------------------------------------------
[A-Z]+                   any character of: "A" to "Z" (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
b                       the boundary between a word char (w)
and something that is not a word char
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
(                        group and capture to 1:
----------------------------------------------------------------------
.+?                      any character except n (1 or more times
(matching the least amount possible))
----------------------------------------------------------------------
)                        end of 1
----------------------------------------------------------------------
(?=                      look ahead to see if there is:
----------------------------------------------------------------------
s                       whitespace (n, r, t, f, and " ")
----------------------------------------------------------------------
in                       "in"
----------------------------------------------------------------------
)                        end of look-ahead
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------