est ici une expression régulière qui va gérer toutes les données que je peux jeter à elle:
(\d++(?! */))? *-? *(?:(\d+) */ *(\d+))?.*$
Cela mettra les chiffres dans les groupes suivants:
- La partie entière du nombre mixte , si elle existe
- Le numérateur, si une fraction sort
- Le dénominateur, si une fraction existe
En outre, voici l'explication RegexBuddy pour les éléments (ce qui m'a énormément aidé lors de la construction il):
Match the regular expression below and capture its match into backreference number 1 «(\d++(?! */))?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single digit 0..9 «\d++»
Between one and unlimited times, as many times as possible, without giving back (possessive) «++»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?! */)»
Match the character “ ” literally « *»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “/” literally «/»
Match the character “ ” literally « *»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “-” literally «-?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character “ ” literally « *»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below «(?:(\d+) */ *(\d+))?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the regular expression below and capture its match into backreference number 2 «(\d+)»
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “ ” literally « *»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “/” literally «/»
Match the character “ ” literally « *»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 3 «(\d+)»
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
J'ai déjà une expression régulière de la solution, et il fonctionne très bien, donc je vais partager avec SO dans l'espoir que cela sauvera beaucoup de travail à quelqu'un d'autre. –
Pour quelle langue et/ou moteur regex est-ce que c'est? –
C'était pour Java, mais avec RegexBuddy, j'aurais pu facilement le faire tourner vers n'importe quel moteur. –