简体   繁体   English

当您有树型模式时,正则表达式无法正常工作

[英]Regex is not working as expected when you have tree patterns

I have this regex: 我有这个正则表达式:

^(?'IF'[IF\s]{3})(?'CONDICTION'[\w\s\d+=/\.\*-><\(\)]+)(?'THEN'THEN)(?'TRUE'[\w\s\d+=/\.\*-><\(\)]*)(?'ELSE'[\s\w]ELSE[\s|\n])(?<FALSE>[\w\s\d.+\-*/]*)(?<ENDIF>([\s\nENDIF]{6}))

For this expression: 对于此表达式:

IF 1 = 1 THEN 1 ELSE 3 ENDIF

Works fine, i got this groups: 工作正常,我得到了这个小组:

IF: IF
CONDICTION: 1 = 1 
THEN: THEN
TRUE: 1
ELSE: ELSE 
FALSE: 3
ENDIF: ENDIF

But if I try this expression: 但是,如果我尝试使用以下表达式:

IF 1 = 1 THEN 1 ELSE IF 1 = 2 THEN 3 ELSE 2 ENDIF ENDIF

I got this groups: 我得到了这些小组:

IF: IF 
CONDICTION: 1 = 1 THEN 1 ELSE IF 1 = 2 
THEN: THEN
TRUE:  3
ELSE:  ELSE 
FALSE: 2 ENDIF
ENDIF:  ENDIF

But I want this: 但是我想要这个:

IF: IF 
CONDICTION: 1 = 1
THEN: THEN
TRUE:  1
ELSE:  ELSE 
FALSE: IF 1 = 2 THEN 3 ELSE 2 ENDIF
ENDIF:  ENDIF

What regex I need to match this? 我需要什么正则表达式来匹配这个?

You seem that you are on the right track. 您似乎在正确的轨道上。 You have your grammar defined so you need to tokenize your expressions and depending on your grammar parse them. 您已经定义了语法,因此需要标记化表达式,并根据语法来解析它们。 In other words you need to write a parser for your particular needs. 换句话说,您需要为您的特定需求编写一个解析器。

Some useful link : 一些有用的链接:

http://www.dreamincode.net/forums/topic/110782-c-beginner-parser-class-tutorial/ http://www.dreamincode.net/forums/topic/110782-c-beginner-parser-class-tutorial/

I solved my problem using Irony 我用反讽解决了我的问题

I will explain details in blog and will edit this answer! 我将在博客中解释详细信息,并将编辑此答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM