ECMAScript正则表达式用于多行字符串 [英] ECMAScript Regex for a multilined string

查看:83
本文介绍了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正则表达式用于多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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