'How to extract number between [ ] by regex

I have a string as below:

53 12/Feb/2022 11:12:08 POST https 200 [1044ms]

I want to get the number in the text [xxxms] -> 1044

I use regex \d+ms, the result is 1044ms, but I want to get only the number.

Please help me with this issue.



Solution 1:[1]

If your regex engine/tool support lookarounds, you could use:

(?<=\[)\d+(?=\w+\])

Demo

Solution 2:[2]

You can get the value without lookarounds using a capture group:

\[(\d+)ms]
  • \[ Match [
  • (\d+) Capture 1+ digits in group 1
  • ms] match literally

Regex demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen
Solution 2 The fourth bird