0

我尝试制作正则表达式来帮助我过滤字符串,例如

blah_blah_suffix

其中 suffix 是长度为 2 到 5 个字符的任何字符串。所以我想接受字符串

blah_blah_aa
blah_blah_abcd

但丢弃

blah_blah_a
blah_aaa
blah_blah_aaaaaaa

我通过以下方式使用 grepl:

samples[grepl("blah_blah_.{2,5}", samples)]

但它忽略了重复的上限(5)。所以它丢弃字符串 blah_blah_a、blah_aaa,但接受字符串 blah_blah_aaaaaaa。

我知道有一种方法可以在不使用正则表达式的情况下过滤字符串,但我想了解如何正确使用 grepl。

4

2 回答 2

2

您需要将表达式绑定到行的开头和结尾:

^blah_blah_.{2,5}$

^匹配行首和行$尾。在此处查看一个工作示例:Regex101

如果要将表达式绑定到字符串的开头和结尾(不是多行),请使用\Aand\Z而不是^and $

锚点教程

于 2014-04-16T17:57:05.053 回答
1
/^[\w]+_[\w]+_[\w]{2,5}$/

演示

Options: dot matches newline; case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]{2,5}»
   Between 2 and 5 times, as many times as possible, giving back as needed (greedy) «{2,5}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
于 2014-04-16T18:04:55.727 回答