How to extract numbers in bracket with regex?

by ryan.murray , in category: Third Party Scripts , a month ago

How to extract numbers in bracket with regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , a month ago

@ryan.murray 

To extract numbers in brackets using regular expressions (regex), you can use the following pattern:

1
[(d+)]


Here's an explanation of the pattern:

  • [ and ]: These are escape characters that match the opening and closing brackets, respectively.
  • (d+): This is a capturing group that matches one or more digits. The parentheses are used to capture the digits inside the brackets.


You can use this regex pattern with your programming language of choice to extract numbers in brackets from a string. Here's an example in Python:

1
2
3
4
5
import re

text = "The numbers in brackets are [123] and [456]."
numbers = re.findall(r'[(d+)]', text)
print(numbers)


This will output:

1
['123', '456']


You can adjust the regex pattern based on your specific requirements or the format of the input text.