使用R语言。它表示括号和括号的意外标记。这种模式似乎也会起作用。我在这里做错了什么?
MainActivity.java
答案:"正则表达式附近没有报价" 但是:它只检查第一个元素。 如何检查向量中的所有元素?
答案 0 :(得分:2)
如果您想检查字符串中是否存在至少一个数字,那么您可以尝试使用它:
str_detect(c("Hello 241", "Whawt 602"), ".*[0-9].*")
或者可能这样:
str_detect(c("Hello 241", "Whawt 602"), "(?=.*[0-9]).*")
如果你需要单独检查每个单词,如果它包含一个数字,那么试试这个:
input <- c("Hello 241", "Whawt 602")
output <- sapply(input, function(x) {
words <- unlist(strsplit(x, "\\s+"))
num_matches <- sapply(words, function(y) str_detect(y, ".*[0-9].*"))
result <- length(words) == sum(num_matches)
return(result)
})
if (sum(output) == 0) {
print("Yay!")
}
else {
print("Oh no!")
}
答案 1 :(得分:2)
也许是这样的:
ifelse(grepl( '[0-9]', c("Hello 241", "Whawt 602",'Nope')),'Oh no!','Yay!')
如果您想一次检查所有元素,只需在all()
周围包裹grepl
:
ifelse(all(grepl( '[0-9]', c("Hello 241", "Whawt 602",'Nope'))),'Oh no!','Yay!')