简体   繁体   中英

Regex to match a pattern with same number in R

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

Like this?

^ma_(\d+)_\1_\1$

See a demo on regex101.com .
This uses backreferences with the first captured group as well as anchors.

Use back-references to make a regex match a previous group again:

ma_(100|[1-9][0-9]?)_\1_\1\b

Regex101 Demo

This will also validate that the numbers are within range. If this validation is unnecessary, use (\\d+) for the capture group.

此答案是对@ 4castle的修改,它将仅提取具有相似数字的字符串。

grep("ma_(100|[0-9][0-9]|[0-9])(_\\1)(_\\1)\\b", stringList, value = T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM