首页 > 解决方案 > R 回溯断言中的正则表达式

问题描述

我正在尝试extract使用tidyr. 我已经在一个正则表达式练习站点中测试了我的正则表达式,该模式似乎有效,并且我使用的是lookbehind assertion.

我有以下示例文本:

=[\"{ Key = source, Values = web,videoTag,assist }\",\"{ Key = type, 
Values = attack }\",\"{ Key = team, Values = 2 }\",\"{ Key = 
originalStartTimeMs, Values = 56496 }\",\"{ Key = linkId, Values = 
1551292895649 }\",\"{ Key = playerJersey, Values = 8 }\",\"{ Key = 
attackLocationStartX, Values = 3.9375 }\",\"{ Key = 
attackLocationStartY, Values = 0.739376770538243 }\",\"{ Key = 
attackLocationStartDeflected, Values = false }\",\"{ Key = 
attackLocationEndX, Values = 1.7897727272727275 }\",\"{ Key = 
attackLocationEndY, Values = -1.3002832861189795 }\",\"{ Key = 
attackLocationEndDeflected, Values = false }\",\"{ Key = lastModified, 
Values = web,videoTag,assist 

我想获取后面的数字attackLocationX(关于攻击位置的任何文本后面的所有数字。

但是,将以下代码与后向断言一起使用,我没有得到任何结果:

df %>% 
extract(message, "x_start",'((?<=attackLocationStartX,/sValues/s=/s)[0- 
9.]+)')

NA如果未找到模式匹配,则此函数将返回,并且我的目标列是所有NA值,尽管已在www.regexr.com. 根据文档,R模式匹配支持后向断言,所以我不确定这里还能做什么。

标签: rregexlookbehind

解决方案


我不确定后面的部分,但在 R 中,您需要转义反斜杠。如果您使用的是非 R 特定的正则表达式检查器,这并不明显。

更多信息在这里

所以你可能希望你的正则表达式看起来像:

"attackLocationStartX,\\sValues\\s=\\s)[0-9.]+"

推荐阅读