用于匹配条件表达式操作语句的正则表达式 [英] Regular expression for matching a conditional expression operational statements

查看:103
本文介绍了用于匹配条件表达式操作语句的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#编写可测试某些文本或字符串的正则表达式.我希望表达式中包含< | > |< = |> = | == | != ??

Hi, I''m trying to write a regular expression in C# that can test some text or strings. I want the expression to have relational statements included < | > |<=|>= | == | != ??

Myexpression = new Regex("^[a-zA-Z_][a-zA-Z0-9_] (< | > | <=| >= | == | !=) ([a-zA-Z_][a-zA-Z0-9_] | \d*) *$");



我希望此表达式匹配任何将两个变量或一个变量与一位数字值进行比较的语句,但它不能按预期工作:(




I want this expression to match any statement that compares two variables or one variable with one digit value, but it does not work as expected:(


Help me Please!

推荐答案

");
");



我希望此表达式匹配任何将两个变量或一个变量与一位数字值进行比较的语句,但它不能按预期工作:(


请帮助我!



I want this expression to match any statement that compares two variables or one variable with one digit value, but it does not work as expected:(


Help me Please!


请记住,每个字符在正则表达式中都是重要的-包括空格.这样可能会更好一些,但是...
Remember that every character is significant in a regex - including spaces. This will probably do a bit better, but...
public static Regex regex = new Regex(
      "[a-zA-Z_][a-zA-Z0-9_]\\s*(<=|>=|==|!=|<|>)\\s*[a-zA-Z_][a-zA"+
      "-Z0-9_]",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.Singleline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );

坦白地说,我感觉到您正在尝试扭曲正则表达式以更好地使用令牌和某种形式的语法检查来完成某些事情.

但是,如果您要继续,我强烈建议您获得一份 Expresso [

To be honest, I get the feeling you are trying to twist a regex to do something that would be better done with tokens and syntax checking of some form.

However, if you are going to continue, I strongly suggest you get a copy of Expresso[^] - it''s free, and it really helps with designing, explaining and test regexes.


正如OriginalGriff所说,这不是正则表达式的最佳目标.您尝试在此处放在一起的正则表达式允许您执行a_ == b_之类的操作,而这可能并非您想要的.它还将您限制为两个字符变量名.

建议您不要考虑使用正则表达式,而将大脑弯曲成不舒服的形状,而建议您考虑使用其他技术,例如词法分析器.有几个免费的词法分析器,但似乎吸引人们的是
ANTLR [^ ] .

另外,您实际上可以花很多时间来创建自己的DSL(特定于域的语言). Microsoft在Visual Studio SDK中提供了有关如何执行此操作的示例.

[为OP编辑]
另一种方法是滚动自己的快速而肮脏的解析器.基本上,您要删除所有空格,然后搜索第一个非字母数字字符(不是下划线);这将是您比较运算符的开始.然后,您将搜索第一个字母数字(或下划线)字符.此时,您将获得变量在哪里以及运算符在哪里的索引.
As OriginalGriff states, this isn''t the best target for a regular expression. The regular expression you are trying to put together here allows you to do things like a_ == b_ which might not be exactly what you want. It also limits you to two character variable names.

Rather than trying to twist the regular expression and bend your brain into uncomfortable shapes in the process, I''d advise you to consider using an alternate technique such as a lexical analyser. There are several freely available lexical analysers, but the one people seem to gravitate towards is ANTLR[^].

Alternatively, you could really go the whole hog and create your own DSL (Domain Specific Language). Microsoft has examples of how to do this in the Visual Studio SDK.


An alternative method would be to roll your own quick and dirty parser. Basically, you''d strip out any spaces then search for the first none alphanumeric character (that wasn''t an underscore); this would be the start of your comparison operator. Then, you''d search for the first alpha-numeric (or underscore) character. At this point, you have the indices of where the variables are, and where the operator is.


这篇关于用于匹配条件表达式操作语句的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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