Zsombi
Zsombi

Reputation: 84

Regex: find occurrence of a digit in a string

Problem: I want to match those strings which contains two digits.Their position is random and a digit should match 2 times.

Example for better understanding my question:

3abc3 
a22de
b7abc7a

For these strings it must match.If a string contains two digits but they are different then it shouldn't match.

Example:

3abcd2 not supposed to match 3abc3 -> supposed to match

I tried using {n}, but it not helps, because it thinks the two number follows each other.

Upvotes: 1

Views: 360

Answers (1)

anubhava
anubhava

Reputation: 785146

You can use this grep:

grep -E '([0-9]).*\1' file

3abc3
a22de
b7abc7a

About this Regex:

([0-9]) # match and capture any digit in group #1
.*      # match 0 or more of any character in between
\1       # using back-reference \1, make sure we have same digit as in group #1

Upvotes: 2

Related Questions