正则表达式匹配的空间,而不是在与QUOT;字符串" [英] Regex matching spaces, but not in "strings"

查看:234
本文介绍了正则表达式匹配的空间,而不是在与QUOT;字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找只有托马斯•空间不括在双引号()定期exression匹配的空间。例如,在

I am looking for a regular exression matching spaces only if thos spaces are not enclosed in double quotes ("). For example, in

Mary had "a little lamb"

应该匹配第一一个第二空间,而不是其他人。

it should match the first an the second space, but not the others.

我要分割字符串的是在其中是不是在双引号的空间,而不是在引号。

I want to split the string only at the spaces which are not in the double quotes, and not at the quotes.

我使用C ++的Qt工具包,并想用的QString ::分(QRegExp)。的QString非常类似于std ::串和QRegExp基本上POSIX正则表达式封装在一个类。如果存在这样的正则表达式,分裂将是微不足道的。

I am using C++ with the Qt toolkit and wanted to use QString::split(QRegExp). QString is very similar to std::string and QRegExp are basically POSIX regex encapsulated in a class. If there exist such a regex, the split would be trivial.

例如:

Mary had "a little lamb"     =>   Mary,had,"a little lamb"
1" 2 "3                      =>   1" 2 "3 (no splitting at ")
abc def="g h i" "j k" = 12   =>   abc,def="g h i","j k",=,12

很抱歉的编辑,我很IM precise当我第一次问这个问题。希望它是有点更清晰了。

Sorry for the edits, I was very imprecise when I asked the question first. Hope it is somewhat more clear now.

推荐答案

(我知道你刚刚张贴几乎一模一样的回答自己,但我不能忍受只是把一切都抛开: - 。/)

(I know you just posted almost exactly the same answer yourself, but I can't bear to just throw all this away. :-/)

如果有可能解决一个正则表达式分割操作您的问题,正则表达式将匹配甚至引号的数字,作为MSalters说。但是,一个分裂的正则表达式应该匹配只有你分裂的空间,所以工作的其余部分有一个超前工作要做。这是我会用什么:

If it's possible to solve your problem with a regex split operation, the regex will have to match even numbers of quotation marks, as MSalters said. However, a split regex should match only the spaces you're splitting on, so the rest of the work has to be done in a lookahead. Here's what I would use:

" +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"

如果文本是很好形成,偶数行情的先行足以确定,刚刚匹配的空间不带引号的序列中。即,lookbehinds是没有必要的,因为QRegExp似乎不支持它们,这是很好。转义引号也可以被容纳,但正则表达式变得很有点大和丑陋。但是,如果你不能确定文本良好的,这是极不可能的,你就可以用来解决你的问题分裂()

If the text is well formed, a lookahead for an even number of quotes is sufficient to determine that the just-matched space is not inside a quoted sequence. That is, lookbehinds aren't necessary, which is good because QRegExp doesn't seem to support them. Escaped quotes can be accommodated too, but the regex becomes quite a bit larger and uglier. But if you can't be sure the text is well formed, it's extremely unlikely you'll be able to solve your problem with split().

顺便说一句,QRegExp确实的的实施 POSIX正前pressions 的--IF它没有,它不支持向前看符号或lookbehinds。相反,它属于松散定义的Perl的正则表达式兼容口味的范畴。

By the way, QRegExp does not implement POSIX regular expressions--if it did, it wouldn't support lookaheads OR lookbehinds. Instead, it falls into the loosely-defined category of Perl-compatible regex flavors.

这篇关于正则表达式匹配的空间,而不是在与QUOT;字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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