Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_R_String_Strsplit - Fatal编程技术网

Regex R:查找数字是否在字符串的范围内

Regex R:查找数字是否在字符串的范围内,regex,r,string,strsplit,Regex,R,String,Strsplit,我有一个字符串s,其中“子字符串”被管道分割。子字符串可能包含数字,也可能不包含数字。我有一个测试字符串n,它包含一个数字,可能包含字母,也可能不包含字母。见下面的例子。请注意,间距可以是任意的 我试图删除n不在范围内或不完全匹配的所有子字符串。我知道我需要按-拆分,转换为数字,并将低/高与n转换为数字进行比较。这是我的出发点,但后来我不得不从unl\u new中获取最终的好字符串 s = "liquid & bar soap 1.0 - 2.0oz | bar 2- 5.0 oz |

我有一个字符串
s
,其中“子字符串”被管道分割。子字符串可能包含数字,也可能不包含数字。我有一个测试字符串
n
,它包含一个数字,可能包含字母,也可能不包含字母。见下面的例子。请注意,间距可以是任意的

我试图删除
n
不在范围内或不完全匹配的所有子字符串。我知道我需要按
-
拆分,转换为数字,并将低/高与
n
转换为数字进行比较。这是我的出发点,但后来我不得不从
unl\u new
中获取最终的好字符串

s = "liquid & bar soap 1.0 - 2.0oz | bar 2- 5.0 oz | liquid soap 1-2oz | dish 1.5oz"
n = "1.5oz"

unl = unlist(strsplit(s,"\\|"))

unl_new = (strsplit(unl,"-"))
unl_new = unlist(gsub("[a-zA-Z]","",unl_new))
期望输出:

"liquid & bar soap 1.0 - 2.0oz | liquid soap 1-2oz | dish 1.5oz"

我是不是完全走错了路?谢谢

不知道它是否足够通用,但您可以尝试:

require(stringr)
splitted<-strsplit(s,"\\|")[[1]]
ranges<-lapply(strsplit(
          str_extract(splitted,"[0-9\\.]+(\\s*-\\s*[0-9\\.]+|)"),"\\s*-\\s*"),
          as.numeric)
tomatch<-as.numeric(str_extract(n,"[0-9\\.]+"))
paste(splitted[
            vapply(ranges, function(x) (length(x)==1 && x==tomatch) || (length(x)==2 && findInterval(tomatch,x)==1),TRUE)],
             collapse="|")
#[1] "liquid & bar soap 1.0 - 2.0oz | liquid soap 1-2oz | dish 1.5oz"
require(stringr)

拆分这里是一个使用r-base的选项

## extract the n numeric
nn <- as.numeric(gsub("[^0-9|. ]", "", n))
## keep only numeric and -( for interval)
## and split by |
## for each interval test the condition to create a boolean vector
contains_n <- sapply(strsplit(gsub("[^0-9|. |-]", "", s),'[|]')[[1]],
       function(x){
         yy <- strsplit(x, "-")[[1]]
         yy <- as.numeric(yy[nzchar(yy)])
         ## the condition
         (length(yy)==1 && yy==nn) || length(yy)==2 && nn >= yy[1] && nn <= yy[2]
       })

## split again and use the boolean factor to remove the parts 
## that don't respect the condition
## paste the result using collapse to get a single character again
paste(strsplit(s,'[|]')[[1]][contains_n],collapse='')

## [1] "liquid & bar soap 1.0 - 2.0oz  liquid soap 1-2oz  dish 1.5oz"
##提取n个数字

nn这里有一个方法,从
unl
步骤开始,使用
stringr

unl = unlist(strsplit(s,"\\|"))
n2 <- as.numeric(gsub("[[:alpha:]]*", "", n))
num_lst <- str_extract_all(unl, "\\d\\.?\\d*")
indx <- lapply(num_lst, function(x) {
  if(length(x) == 1) {isTRUE(all.equal(n2, as.numeric(x))) 
  } else {n2 >= as.numeric(x[1]) & n2 <= as.numeric(x[2])}})

paste(unl[unlist(indx)], collapse=" | ")
[1] "liquid & bar soap 1.0 - 2.0oz  |  liquid soap 1-2oz  |  dish 1.5oz"
unl=unlist(strsplit(“\\\\”)

n2如果
n=2.3oz
,则输出仍然包括
碟式1.5oz
谢谢!现在可以正常工作,
length()
存在于哪个包中?在
帮助中找不到它
这是R3.2.0中的一个基本函数它可以替换为
unlist(lappy(num_lst,length))==1
适用于
1.5oz
,产生
NA | NA
适用于
2.3oz
更新为
isTRUE