Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching only 5xx using regex

Tags:

regex

grep

bash

I want to find all numbers that are in between 500-599. I'm very new to regex, I came up with this :

5[0-9][0-9]+

This is working fine, matching 566,577,500. But it also matches 6578. Which I don't want.

Edit:

Here is my file contents:

asd 554
sad
sads

dsa


456

sa
d

dsa
asda
d500
521
519 asdasd
524 asdasdsdsadsdasd sadsadsadasdsd asdsa dsa dsadsad sad asdas dsa sad sad asds a 543

As many suggested I tried :

grep "^5[0-9]{2}$" test

which isn't finding any numbers at all!

How do I put a constraint on this?

like image 447
batman Avatar asked Feb 04 '26 22:02

batman


1 Answers

If you want to match 5xx only on a line, and not when 5xx occurs as a part of x5xx,

^5\d{2}$

\d = Digit

^ = beginning of line

$ = end of line

EDIT:

Based on additional details in the question, you have a variable number of spaces at the beginning of the line, so, you want the following instead:

\s*5\d{2}\s

Matches spaces on either side of 5xx.

like image 173
Anirudh Ramanathan Avatar answered Feb 06 '26 13:02

Anirudh Ramanathan