[英]boost regex to match IF statements
我需要編寫一個 boost 正則表達式來匹配以下字符串,並根據 IF 塊的參數將其分成三個標記
=IF(ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE))
理想情況下,這些應該來
token1 = "ISNUMBER(SEARCH("Windows",GETWORKSPACE(1)))"
token2 = "ON.TIME(NOW()+"00:00:02","abcdef")"
token3 = "CLOSE(TRUE)"
我最初寫了一個簡單的正則表達式 "(?<=\=IF\()(. ),(. ),(.*)(?=\))" 給出不正確的標記,因為貪婪的限定符需要太多的第一個令牌。 我目前正在
token1 = "ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02""
token2 = ""abcdef")"
token3 = "CLOSE(TRUE)"
還嘗試"(?<=\\=IF\\()([A-Za-z(),:\"]*?),([A-Za-z(),.:\"]*?),([AZ(),:\"]*?)(?=\\))"
沒有運氣。有人可以建議一個正則表達式嗎?
你需要一個簡單的解析器。
這是我最喜歡的用於快速解析器的 Boost 瑞士軍刀。
我創建了一個非常靈活的“標記”語法,它尊重(嵌套)括號和雙引號字符串文字(可能帶有嵌入的轉義引號和括號):
token = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| lexeme[ + ~char_(")]}([{\"',") ]
) ];
其中 token_list 和 string_literal 定義為
string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
token_list = token % ',';
現在=IF(condition, true_part, false_part)
的解析器表達式很簡單:
if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
為了好玩,我使
IF
關鍵字不區分大小寫
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <iostream>
#include <iomanip>
namespace x3 = boost::spirit::x3;
namespace parser {
using namespace x3;
static rule<struct token_, std::string> const token = "token";
static auto const string_literal = lexeme [
'"' >> *('\\' >> char_ | ~char_('"')) >> '"'
];
static auto const token_list = token % ',';
static auto const token_def = raw [ *(
'(' >> -token_list >> ')'
| '[' >> -token_list >> ']'
| '{' >> -token_list >> '}'
| string_literal
| +~char_(")]}([{\"',") // glue together everything else
) ];
BOOST_SPIRIT_DEFINE(token)
static auto const if_expr
= '=' >> no_case["if"]
>> '(' >> token >> ',' >> token >> ',' >> token >> ')';
}
int main() {
for (std::string const& input : {
R"(=IF(ISNUMBER,ON.TIME,CLOSE))",
R"(=IF(ISNUMBER(SEARCH("Windows")),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
R"(=IF(ISNUMBER(SEARCH("Windows",GETWORKSPACE(1))),ON.TIME(NOW()+"00:00:02","abcdef"),CLOSE(TRUE)))",
" = if( isnumber, on .time, close ) ",
R"( = if( "foo, bar", if( isnumber, on .time, close ), IF("[ISN(UM}B\"ER")) )",
})
{
auto f = input.begin(), l = input.end();
std::cout << "=== " << std::quoted(input) << ":\n";
std::string condition, true_part, false_part;
auto attr = std::tie(condition, true_part, false_part);
if (phrase_parse(f, l, parser::if_expr, x3::blank, attr)) {
std::cout << "Parsed: \n"
<< " - condition: " << std::quoted(condition) << "\n"
<< " - true_part: " << std::quoted(true_part) << "\n"
<< " - false_part: " << std::quoted(false_part) << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f!=l) {
std::cout << "Remaining unparsed: " << std::quoted(std::string(f,l)) << "\n";
}
}
}
印刷
=== "=IF(ISNUMBER,ON.TIME,CLOSE)":
Parsed:
- condition: "ISNUMBER"
- true_part: "ON.TIME"
- false_part: "CLOSE"
=== "=IF(ISNUMBER(SEARCH(\"Windows\")),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\"))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== "=IF(ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1))),ON.TIME(NOW()+\"00:00:02\",\"abcdef\"),CLOSE(TRUE))":
Parsed:
- condition: "ISNUMBER(SEARCH(\"Windows\",GETWORKSPACE(1)))"
- true_part: "ON.TIME(NOW()+\"00:00:02\",\"abcdef\")"
- false_part: "CLOSE(TRUE)"
=== " = if( isnumber, on .time, close ) ":
Parsed:
- condition: "isnumber"
- true_part: "on .time"
- false_part: "close "
=== " = if( \"foo, bar\", if( isnumber, on .time, close ), IF(\"[ISN(UM}B\\\"ER\")) ":
Parsed:
- condition: "\"foo, bar\""
- true_part: "if( isnumber, on .time, close )"
- false_part: "IF(\"[ISN(UM}B\\\"ER\")"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.