[英]Regex in c++ for maching some patters
I want regex of this.我想要这个的正则表达式。
I want to implement this.我想实现这个。 But bit confused, how to, as I am newbie in using Regex.
但是有点困惑,因为我是使用 Regex 的新手。 Can anyone share these Regex?
任何人都可以分享这些正则表达式吗?
If this is a longer project and will have more requirements later, then definitely a different approach would be better.如果这是一个较长的项目并且以后会有更多的要求,那么采用不同的方法肯定会更好。
The standard approach to solve such a problem ist to define a grammar and then created a lexer and a parser.解决此类问题的标准方法是定义语法,然后创建词法分析器和解析器。 The tools lex/yacc or flex/bison can be used for that.
lex/yacc 或 flex/bison 工具可以用于此。 Or, a simple shift/reduce parser can also be hand crafted.
或者,也可以手工制作一个简单的移位/归约解析器。
The language that you sketched with the given grammar, may be indeed specified with a Chomsky class 3 grammar, and can hence be produced gy a regular grammar.你用给定语法描绘的语言,可能确实用 Chomsky class 3 语法指定,因此可以用常规语法生成。 And, with that, parsed with regular expressions.
然后,用正则表达式进行解析。
The specification is a little bit unclear as to what a register is and if there are more keyowrds.关于寄存器是什么以及是否有更多关键字,规范有点不清楚。 Especially ecall is unclear.
尤其是ecall不清楚。
But how to build such a regex?但是如何构建这样的正则表达式呢?
You will define small tokens and concatenate them.您将定义小标记并将它们连接起来。 And different paths can be implemented with the or operator
|
并且不同的路径可以用 or 运算符
|
来实现. .
Let's give sume example.让我们举个例子。
a\d+
.a\d+
匹配。 So, an "a" followed by ome digits.[az]\d+
[az]\d+
|
|
列出。 . add|sub
add|sub
\s+
or [ ]+
or whatever spaces you need.\s+
或[ ]+
或您需要的任何空格。|
|
for the complete pathAnd with that, one of many many possible solutions can be:因此,许多可能的解决方案之一可以是:
^[ ]*((add|sub)[ ]+(a\d+)[ ]*,[ ]*(a\d+)[ ]*,[ ]*(a\d+)|(ecall))[ ]*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.