Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式,后面跟数字时如何替换第一个字母_Regex_R - Fatal编程技术网

Regex 正则表达式,后面跟数字时如何替换第一个字母

Regex 正则表达式,后面跟数字时如何替换第一个字母,regex,r,Regex,R,我有一个数据帧,我想在其中根据变量的值转换变量 如何获取包含以“B+数字”开头的值的行的位置,然后将其替换为“BLA+相同数字” 更确切地说,这里有一个我想用“BLAB92”替换的例子:“B92MEL-word2” 我试过: grep("^B[[:digit:]]*", dataset$variable) 它会查找“B+数字”之类的值,但也会查找我不想要的值: BLA192/MEL poword2 BLA25879 - blabla 试一试 更新 数据 str1我建议您在使用它

我有一个数据帧,我想在其中根据变量的值转换变量

如何获取包含以“B+数字”开头的值的行的位置,然后将其替换为“BLA+相同数字”

更确切地说,这里有一个我想用“BLAB92”替换的例子:“B92MEL-word2”

我试过:

  grep("^B[[:digit:]]*", dataset$variable) 
它会查找“B+数字”之类的值,但也会查找我不想要的值:

  BLA192/MEL poword2
  BLA25879 - blabla
试一试

更新 数据
str1我建议您在使用它之前查看一下
?rep
。预期的输出是什么?我想在我的输出中显示包含以“B+数字”开头的字符串的行数。很抱歉,函数是“grep”而不是“rep”。谢谢您的回答,我想要类似“BLA92”的东西,而不是BLAB92“,怎么做?@SparkUser更新了帖子,请检查。谢谢,但是现在如何直接修改我的数据帧?因为这只返回一个修改过的向量。。。(在我的例子中,str1是data$variable)@SparkUser,尝试
data$variable
sub('^(B\\d+).*', 'BLA\\1', str1)
#[1] "BLA192/MEL poword2" "BLA25879 - blabla"  "BLAB92"   
 sub('^B(\\d+).*', 'BLA\\1', str1)
 #[1] "BLA192/MEL poword2" "BLA25879 - blabla"  "BLA92"         
str1 <- c('BLA192/MEL poword2', 'BLA25879 - blabla', 'B92MEL-word2')