gpt4 book ai didi

正则表达式匹配马虎分数/混合数字

转载 作者:行者123 更新时间:2023-12-03 23:44:55 25 4
gpt4 key购买 nike

我有一系列包含混合数字的文本(即:整个部分和小数部分)。问题是文本充满了人为编码的草率:

  • 整个部分可能存在也可能不存在(例如:“10”)
  • 小数部分可能存在也可能不存在(例如:“1/3”)
  • 这两个部分可以用空格和/或连字符分隔(例如:“10 1/3”、“10-1/3”、“10 - 1/3”)。
  • 分数本身在数字和斜杠之间可能有也可能没有空格(例如:“1/3”、“1/3”、“1/3”)。
  • 小数后面可能还有其他文字需要忽略

  • 我需要一个可以解析这些元素的正则表达式,以便我可以从这个困惑中创建一个适当的数字。

    最佳答案

    这是一个正则表达式,它将处理我可以抛出的所有数据:

    (\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) «$»

    关于正则表达式匹配马虎分数/混合数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/245345/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com