正则表达式用于C中的字符串 [英] Regex For Strings in C

查看:74
本文介绍了正则表达式用于C中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C中的某些字符串制作正则表达式.

I'm looking to make a regular expression for some strings in C.

这是我到目前为止所拥有的:

This is what i have so far:

C中的字符串用双引号()分隔,因此正则表达式必须用\" \包围.

Strings in C are delimited by double quotes (") so the regex has to be surrounded by \" \".

该字符串可能不包含换行符,因此我需要执行[^ \ n](我认为).

The string may not contain newline characters so I need to do [^\n] ( I think ).

当且仅当转义时,字符串也可以包含双引号或反斜杠字符.因此[\\ \](我还是想).

The string may also contain double quotes or back slash characters if and only if they're escaped. Therefore [\\ \"] (again I think).

除此之外,其他任何事情.

Other than that anything else goes.

非常感谢任何帮助,我对如何开始编写此正则表达式有点迷茫.

Any help is much appreciated I'm kind of lost on how to start writing this regex.

推荐答案

一种简单的flex模式,用于识别字符串文字(包括具有嵌入式行连续性的文字):

A simple flex pattern to recognize string literals (including literals with embedded line continuations):

["]([^"\\\n]|\\.|\\\n)*["]

这将允许

   "string with \
line continuation"

但不是

"C doesn't support
 multiline strings"

如果您不想处理连续行,请删除 \\\ n 替代项.如果需要三方字母支持,那么会更加烦人.

If you don't want to deal with line continuations, remove the \\\n alternative. If you need trigraph support, it gets more irritating.

尽管可以识别字符串,但它不会尝试去理解它们.通常,C lexer希望处理带有反斜杠序列的字符串,以便将"\" \ n"转换为两个字符" NL ( 0x22 0x0A ).您可能会想要看一下,例如,优化flex字符串文字解析(尽管如果您使用C语言进行编程,则需要对其进行修改).

Although that recognizes strings, it doesn't attempt to make sense of them. Normally, a C lexer will want to process strings with backslash sequences, so that "\"\n" is converted to the two characters "NL (0x22 0x0A). You might, at some point, want to take a look at, for example, Optimizing flex string literal parsing (although that will need to be adapted if you are programming in C).

Flex模式记录在 flex手册中.也许值得一读有关正则表达式的良好参考,例如John Levine在

Flex patterns are documented in the flex manual. It might also be worthwhile reading a good reference on regular expressions, such as John Levine's excellent book on Flex and Bison.

这篇关于正则表达式用于C中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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