user3729399
user3729399

Reputation: 139

Regular expression in grep that match string

I have strings that looks like:

sapaa-czbrqrnop-3449607e001
sapaa-deduipswp-3446871e001
sapaa-delejwtbp2-3446869e001

These strings are in files on server. I would like to use a grep a find this strings. So I wrote something like this:

grep "[a-zA-Z]\-[a-zA-Z0-9]\-[0-9]\e[0-9]" filename

So it should find appropriate strings on lines. But it doesn't work. I am on the rocks. Is there anything wrong with this regular expression or I can not use it this way in grep?

Upvotes: 0

Views: 966

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

You just need to add \+ or * next to each character classes. + repeats the previous token one or more times, whereas your character class [a-zA-Z] without following * or + would match a single character. * repeats the previous token zero or more times. Because basic grep uses BRE , you need to escape the + symbol. So that it would act like a repeatation quantifier.

grep "[a-zA-Z]\+-[a-zA-Z0-9]\+-[0-9]\+e[0-9]\+" filename

Use anchors if necessary.

grep "^[a-zA-Z]\+-[a-zA-Z0-9]\+-[0-9]\+e[0-9]\+$" filename

Upvotes: 2

Related Questions