rbrow
rbrow

Reputation: 13

Extended regular expressions - Think I might be too restrictive in my search but not sure

I'm trying to figure out this extended expression to search a txt file for specific lines using the following parameters:

These should match:

x555-1212x
x555__1212x
x555-_-1212x 

but these would not match:

x999555-1212x
x555-1212999x x999555-1212999x
555-121x
x55-1212
5551212

Here's what I've tried and it's giving me 5 out of the 7 required lines according to a checking script

egrep '[^0-9]+[[:digit:]]{3}[-_]+[[:digit:]]{4}[^0-9]+' foo.txt

I'm not sure where I'm going wrong with this...whether I'm being too restrictive and eliminating certain white space characters like Tab or something else. Anyone have any ideas?

Upvotes: 1

Views: 131

Answers (1)

CSᵠ
CSᵠ

Reputation: 10169

Since you're interested in lines that have your pattern you could use start ^ and end $ RegEx operators in your matching pattern.

RegEx: ^.*?(\D|^)(\d{3}[-_]+\d{4})(\D|$).*?$
Explained demo here: http://regex101.com/r/gQ0cE2

Upvotes: 1

Related Questions