",(?!.* \\))"返回“无效的正则表达式";R中的错误 [英] ",(?!.*\\))" returning "Invalid Regex" error in R

查看:88
本文介绍了",(?!.* \\))"返回“无效的正则表达式";R中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的字符串,我试图仅选择括号外的逗号,以便可以基于该字符串分割字符串.这是我正在使用的字符串:

I've got a string that I'm working with and I'm trying to select only the commas that are outside of the parentheses so that I can split the string based on that. Here's the string I'm working with:

"LIVINGSTON(布莱顿,迪尔菲尔德,热那亚,哈特兰,奥索拉和泰隆的乡镇,马科姆,门罗,奥克兰,萨尼拉克,圣克莱尔和韦恩县)

"LIVINGSTON (Townships of Brighton, Deerfield, Genoa, Hartland,, Oceola & Tyrone), MACOMB, MONROE, OAKLAND, SANILAC, ST. CLAIR, AND WAYNE COUNTIES"

我正在尝试使用问题标题中提到的正则表达式,它告诉我这是无效的.大概这是因为R认为应该转义的右括号被圆括号括起来,从而使匹配组关闭,因此第二个括号将所有内容都丢掉了.我只是想知道如何解决此问题.这是我使用的语法:

I'm trying to use the regex mentioned in the question title and it's telling me that it's not valid. Presumably this is because the closing parenthesis that is supposed to be escaped is being recognized by R as the parenthesis closing the match group and so the second parenthesis is throwing everything off. I'm just curious about how to work around this. Here is the syntax I'm using:

counties <- "LIVINGSTON (Townships of Brighton, Deerfield, Genoa, Hartland,, Oceola & Tyrone), MACOMB, MONROE, OAKLAND, SANILAC, ST. CLAIR, AND WAYNE COUNTIES"
tmp <- strsplit(counties, ',(?!.*\\))')

我显然可以做我现在正在做的事情的逆过程,而不是将括号内的逗号分隔为文本,而只需替换括号内的逗号,然后将逗号分隔为字符串,但是我d想知道为什么这行不通.

I can obviously just do the inverse of what I'm doing now and instead of splitting the text on the commas outside of the parentheses, simply replace the commas inside of the parentheses and then split the string on commas, but I'd like to know why this isn't working.

推荐答案

我相信您的正则表达式无法正常工作的原因是因为它非常Perl式,需要perl = T标志.我认为这还有些不正确,因为您应该检查开括号和圆括号是否完整...我认为这是一种通用的解决方案,不仅适合您的特定情况:

I believe the reason your regex isn't working is because it's very Perl-ish, which requires the perl=T flag. I think it is also slightly malformed in that you should check for opening and closing parentheses to be complete... I think this is a general solution matching not just your specific case:

counties <- "LIVINGSTON (Townships of Brighton, Deerfield, Genoa, Hartland,, Oceola & Tyrone), MACOMB, MONROE, OAKLAND, SANILAC, ST. CLAIR, AND WAYNE COUNTIES"
tmp <- strsplit(counties, ",(?![^(]*\\))", perl=T)

这篇关于&quot;,(?!.* \\))&quot;返回“无效的正则表达式";R中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆