如何在c ++ regex(自由间隔模式)中忽略标记之间的空格 [英] How do I ignore whitespace between tokens in c++ regex (free spacing mode)

查看:131
本文介绍了如何在c ++ regex(自由间隔模式)中忽略标记之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建大型正则表达式查询,并且不希望正则表达式在空格不匹配时失败。适用于我的应用程序的示例是,我想要一个与以下两个匹配的regex表达式:

I am building large regex queries and don't want the regex to fail on a whitespace mismatch. An example applicable to my application would be that I want a regex expression that matches both of the following:

int a;
int a;

c $ c> int\s + a; ,但谁想要读100行!

I can match both of these using int\s+a;, but who wants to read through 100 lines of that!

搜索并发现自由间距模式 http:/ /www.regular-expressions.info/freespacing.html ),它似乎使 int a; 匹配上述两个。大!如何在C ++中使用标准的 regex 库?

So I did a quick search and discovered free spacing mode (http://www.regular-expressions.info/freespacing.html) which seems to make int a; match both of the above. Great! How do I use it in C++ using the standard's regex library?

Boost也是可以接受的,

Boost is also acceptable, but an std solution would be preferred.

推荐答案

std regex库基于POSIX正则表达式语法。根据你的链接,这是一个不支持自由间隔模式。

The std regex library is based on the POSIX regular expression grammar. According to your link, that is one that doesn't support free spacing mode.

在本教程讨论的风格,只有XML模式和POSIX和GNU风格不支持它

"Of the flavors discussed in this tutorial, only XML Schema and the POSIX and GNU flavors don't support it"

我相信你应该使用\s +。如果你正在生成静态表达式,我的建议是使用boost :: xpressive。

I believe you should use \s+. If you are generating static expressions, my advice would be to use boost::xpressive.

请记住,阅读复杂的正则表达式是一项需要大量练习的技能。如果你认为\s +是一个问题...只是你等待。

Bear in mind that reading a complex regex is a skill that takes a lot of practice. If you think \s+ is a problem... just you wait.

这篇关于如何在c ++ regex(自由间隔模式)中忽略标记之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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