這裏有一個正則表達式將處理所有的數據,我可以扔了:
(\d++(?! */))? *-? *(?:(\d+) */ *(\d+))?.*$
這將會把數字分爲以下幾組:
- 混合數字的整數部分如果它存在
- 的分子,如果一小部分退出
- 分母,如果一小部分存在
而且,這裏的元素使用RegexBuddy說明(施工時它這讓我非常):
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) «$»
我已經有了一個解決方案正則表達式,它的作品真的很好,所以我打算與分享所以希望它能夠拯救別人很多工作。 – 2008-10-29 00:14:14
它使用哪種語言和/或正則表達式引擎? – 2008-10-30 04:21:33