How to extract numbers in bracket with regex?

by ryan.murray , in category: Third Party Scripts , 5 months ago

How to extract numbers in bracket with regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 5 months 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.

Related Threads:

How to dynamically extract numbers before text with regex?
How to extract only numbers from string in mysql?
How to use regex with numbers?
How to extract a substring of a url with regex?
How to ignore an character with random numbers using regex?
How to extract part of string using regex?