Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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/7/elixir/2.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 删除R中字母数字字符的前导零_Regex_String_R_Character_Gsub - Fatal编程技术网

Regex 删除R中字母数字字符的前导零

Regex 删除R中字母数字字符的前导零,regex,string,r,character,gsub,Regex,String,R,Character,Gsub,我有一个带有字母数字字符的字符向量d d <- c("012309 template", "separate 00340", "00045", "890 098", "3405 garage", "matter00908") d [1] "012309 template" "separate 00340" "00045" "890 098" "3405 garage" "matter00908" 以下是利用软件包中的stri\u rep

我有一个带有字母数字字符的字符向量
d

d <- c("012309 template", "separate 00340", "00045", "890 098", "3405 garage", "matter00908")

d
[1] "012309 template" "separate 00340"  "00045"           "890 098"         "3405 garage"     "matter00908"  

以下是利用软件包中的
stri\u replace\u all\u regex
的解决方案:


除非前面有数字,否则可以使用负回溯消除0:

> d <- c("100001", "012309 template", "separate 00340", "00045", "890 098", "3405 garage", "matter00908")
> gsub("(?<![0-9])0+", "", d, perl = TRUE)
[1] "100001"         "12309 template" "separate 340"   "45"            
[5] "890 98"         "3405 garage"    "matter908"     

“\\d+”怎么办?编辑:
\d
表示任何数字,
\d+
表示它们的非空序列这也在删除数字字符串中的多个零,例如100001到101。@现在应该修复作物。
d <- c("012309 template", "separate 00340", "00045",
       "890 098", "3405 garage", "matter00908")
library("stringi")
stri_replace_all_regex(d, "\\b0*(\\d+)\\b", "$1")
## [1] "12309 template" "separate 340"   "45"             "890 98"
## [5] "3405 garage"    "matter00908"   
stri_replace_all_regex(d, "0*(\\d+)", "$1")
## [1] "12309 template" "separate 340"   "45"             "890 98"
## [5] "3405 garage"    "matter908"  
> d <- c("100001", "012309 template", "separate 00340", "00045", "890 098", "3405 garage", "matter00908")
> gsub("(?<![0-9])0+", "", d, perl = TRUE)
[1] "100001"         "12309 template" "separate 340"   "45"            
[5] "890 98"         "3405 garage"    "matter908"     
> gsub("(^|[^0-9])0+", "\\1", d, perl = TRUE)
[1] "100001"         "12309 template" "separate 340"   "45"            
[5] "890 98"         "3405 garage"    "matter908"     
>