简体   繁体   中英

Regex get value between parentheses

I have a line like: 10 lessons (40h 25m) and I need to get the duration value and duration unit between parentheses using two regexps. 1st regex to get dur. value and second for unit.

Example:

  1. 10 lessons (40h 25m) - get 40 and h
  2. 2 lessons (50m) - get 50 and m
  3. 25 lessons (3d 10h 23m) - get 3 and d

Note:

I can't use one regex for this purpose. My JSON structure:

{
  "duration": {
    "unit": {
      "regex": "<regex>"
      //props..
    },
    "value": {
      "regex": "<regex>"
      //props..
    }
  }
}

EDIT: I used regexp2 engine for getting the functionality of lookarounds.

You may try the below regex's to get your requirements:

(?<=\()\d+ --> to get the duration
(?<=\(\d+)[a-zA-Z] --> to get the units

Explanation of the above regex:

(?<=\()\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.

图片

You can find the demo of the above regexes in here and here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM