ECMAScript正则表达式用于多行字符串

编程入门 行业动态 更新时间:2024-10-25 20:27:41
本文介绍了ECMAScript正则表达式用于多行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为我的应用程序编写加载过程,它涉及从文件读取数据并创建具有适当属性的适当对象。

I am writing the loading procedure for my application and it involves reading data from a file and creating an appropriate object with appropriate properties.

该文件由顺序条目组成(用换行符分隔),格式如下:

The file consists of sequential entries (separated by a newline) in the following format:

=== OBJECT TYPE === <Property 1>: Value1 <Property 2>: Value2 === END OBJECT TYPE ===

其中的值通常是字符串,可能由任意字符,换行符等组成。

Where the values are often strings which may consist of arbitrary characters, new-lines, etc.

我想创建一个 std :: regex 可以匹配此格式,并允许我使用 std :: regex_iterator 将每个对象读入文件

I want to create a std::regex which can match this format and allow me to use std::regex_iterator to read each of the objects into the file in turn.

但是,我在创建与这种格式匹配的正则表达式时遇到了麻烦;我已经按照以下方式查看了ECMAScript语法并创建了正则表达式,但是它与测试应用程序中的字符串不匹配:

However, I am having trouble creating a regex which matches this type of format; I have looked at the ECMAScript syntax and create my regex in the following way, but it does not match the string in my test application:

const std::regex regexTest( "=== ([^=]+) ===\\n([.\\n]*)\\n=== END \\1 ===" );

在以下测试应用程序中使用它时,它无法将正则表达式与字符串匹配:

And when using this in the following test application, it fails to match the regex to the string:

int main() { std::string testString = "=== TEST ===\n<Random Example>:This is a =test=\n<Another Example>:Another Test||\n=== END TEST ==="; std::cout << testString << std::endl; const std::regex regexTest( "=== ([^=]+) ===\\n([.\\n]*)\\n=== END \\1 ===" ); std::smatch regexMatch; if( std::regex_match( testString, regexMatch, regexTest ) ) { std::cout << "Prefix: \"" << regexMatch[1] << "\"" << std::endl; std::cout << "Main Body: \"" << regexMatch[2] << "\"" << std::endl; } return 0; }

推荐答案

您的问题比它看起来。此:

Your problem is quite simpler than it looks. This:

const std::regex regexTest( "=== ([^=]+) ===\\n((?:.|\\n)*)\\n=== END \\1 ===" );

在clang ++ / libc ++上工作完美。似乎 \n 不适合ECMAscript regexen中的 [] 括号。如果要在regex_search 中查找多个regex实例,请记住在regex_search 时使用而不是 regex_match 。字符串!

worked perfectly on clang++/libc++. It seems that \n does not fit into [] brackets in ECMAscript regexen. Remember to use while regex_search instead of if regex_match if you want to look for more than one instance of the regex inside the string!

更多推荐

ECMAScript正则表达式用于多行字符串

本文发布于:2023-11-03 12:58:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   ECMAScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!