我有这个词,"sam buy expensive toys as 125898652"
。我想在“ as”之后提取单词,即“ 125898652”。
我正在使用
(?<=as\s)+[^\s]+
我已经在https://regex101.com/r/NaWAl1/1上尝试过,并且效果很好。当我在R上执行它时,返回错误为
Error: '\s' is an unrecognized escape in character string starting ""(?<='as'\s"
所以我将其修改为
(?<='CR'\s)+[^\s]+
它返回不同的错误为:
Error in stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) :
Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)
有人可以向我解释为什么正则表达式在R中不同,以及如何使其正常工作。非常感谢
stringr::str_extract("sam buy expensive toys as 125898652", "(?<=as\\s)\\w+")
#[1] "125898652"
对于其中具有
,
且可能具有小数位的单词,我们可以做
x <- "sam buy expensive toys as 128984,45697.00"
sub(".*as\\s(\\d+\\.?\\d+).*", "\\1",gsub(',', '', x))
#[1] "12898445697.00"
> unlist(strsplit(s,split = "(?<=as\\s)",perl = T))[2]
[1] "125898652"