gpt4 book ai didi

regex - 正则表达式在括号之间获取值

转载 作者:行者123 更新时间:2023-12-01 21:16:58 25 4
gpt4 key购买 nike

我有一行像:10 lessons (40h 25m),我需要使用两个正则表达式获取括号之间的持续时间值和持续时间单位。第一个正则表达式获得dur。值和秒为单位。
示例:

  • 10 lessons (40h 25m)-获取 40 h
  • 2 lessons (50m)-获取 50 m
  • 25 lessons (3d 10h 23m)-获取 3 d

  • 注意:
    我不能为此使用一个正则表达式。我的JSON结构:
    {
    "duration": {
    "unit": {
    "regex": "<regex>"
    //props..
    },
    "value": {
    "regex": "<regex>"
    //props..
    }
    }
    }
    编辑:我使用 regexp2引擎来获取环视功能。

    最佳答案

    您可以尝试以下正则表达式来满足您的要求:

    (?<=\()\d+ --> to get the duration
    (?<=\(\d+)[a-zA-Z] --> to get the units
    以上正则表达式的解释:

    (?<=\()\d+ - Represents positive look behind matching one or more digit just before a (.

    [a-zA-Z] - Represents a single character which is just followed by ( along with one or more digits.


    pict
    您可以在 herehere.中找到上述正则表达式的演示。

    关于regex - 正则表达式在括号之间获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62321316/

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