【发布时间】:2019-02-10 18:04:21
【问题描述】:
我有一个模型公式(作为字符串)并且想要提取特定参数的值,在我的例子中是 id。现在我找到了一种返回字符串 而没有 所需字符串值的方法。我想要完全相反,我只想要我的结果中缺少的字符串值:
xx <- "gee(formula = breaks ~ tension, id = wool, data = warpbreaks)"
sub("(?=(id=|id =))([a-zA-Z].*)(?=,)", "\\1", xx, perl =T)
#> [1] "gee(formula = breaks ~ tension, id =, data = warpbreaks)"
返回值中缺少wool,但我只想将wool 作为结果字符串...谁能帮我找到正确的正则表达式模式?
【问题讨论】:
-
这样就可以了:
sub(".*id ?= ?(.*?),.*", "\\1", xx)。您需要匹配整个字符串。 -
像魅力一样工作,非常感谢!