Home > other >  Regular expression to get the text inside parentheses and match a specified word
Regular expression to get the text inside parentheses and match a specified word

Time:07-01

I'm trying to get the string inside parentheses but match 'min' or 'mins'. For example, if I have:

 - Closing words (3 min.) 
 - Demonstration (Part One)

I Only want to get "3 min" not "Part One". Can someone help me? I'm using python

CodePudding user response:

(?<=\()[^)]*\bmins?\b[^)]

You can try something like this.

See demo.

https://regex101.com/r/Zd4nAs/1

CodePudding user response:

Since the 3 in "3 min" is a digit, you can use:

import re

my_str = """
- Closing words (3 min.) 
- Demonstration (Part One)"""


print(re.search(r"\d \smin", my_str).group(0))

Output:

3 min
  • Related