Reputation: 1139
I have set of strings which looks like the below. Each string has 3 numbers separated with an underscore (_). Each number is a value between 1 - 100.
ma_1_1_1
ma_2_100_59
ma_29_29_29
ma_100_100_100
ma_7_72_78
ma_10_10_100
ma_4_4_49
I want to write a regular expression where I can get the strings whose digits are all same. For example my output would be
ma_1_1_1, ma_29_29_29 and ma_100_100_100
Upvotes: 0
Views: 272
Reputation: 43169
Like this?
^ma_(\d+)_\1_\1$
See a demo on regex101.com.
This uses backreferences with the first captured group as well as anchors.
Upvotes: 5
Reputation: 1139
This answer is a modification to @4castle which will only extract the strings with similar numbers.
grep("ma_(100|[0-9][0-9]|[0-9])(_\\1)(_\\1)\\b", stringList, value = T)
Upvotes: 0
Reputation: 33466
Use back-references to make a regex match a previous group again:
ma_(100|[1-9][0-9]?)_\1_\1\b
This will also validate that the numbers are within range. If this validation is unnecessary, use (\d+)
for the capture group.
Upvotes: 3