正则表达式 - 仅匹配括号内的分隔符

时间:2017-02-24 09:53:42

标签: regex

我有一个解析CSV文件的程序。不幸的是,如果分隔符在括号内,程序无法处理。现在我想找到一个正则表达式,如果它在括号内,则找到它。

int

以下Regex返回整个注释字段,但我只想要;括号内的字符

Name;Zip;Comment
Smith,12345;"Weird comment with ; inside"

2 个答案:

答案 0 :(得分:0)

你可以试试像这样的正则表达式

r\".*(;).*"\

在这种情况下,正则表达式将与整个注释匹配。但是,捕获组1(括号内的部分)将与双引号内的;匹配。

答案 1 :(得分:0)

您应该使用以下 regex 来捕获整个注释,并使用捕获的组替换分号:

(".*?)(;\s)(.*?")

input        >>  Name;Zip;Comment
                 Smith,12345;"Weird comment with ; inside"
match        >>  (".*?)(;\s)(.*?")
replace with >>  $1$3
output       >>  Name;Zip;Comment
                 Smith,12345;"Weird comment with inside"

请参阅demo / explanation