正则表达式需要在括号之间读入 [英] Regular expression needed to read in between the parenthesis

查看:88
本文介绍了正则表达式需要在括号之间读入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是正则表达式的新手。我需要一个正则表达式来读取两个括号之间的值。基本上,正则表达式应该从逻辑表达式中获取操作数。



例如:



INPUT:



1.((infx.Length!= i)&&(ch = infx [i ++])!='\ 0')

2.(infx.Length!= i && infx [i ++])!='\ 0')





OUTPUT :



1. infx.Length!= i

ch = infx [i ++])!='\ 0'



2. infx.Length!= i

infx [i ++])!='\ 0'



先谢谢

Hi Everyone,

I am new to regular expressions. I need a regular expression to read the values between two parenthesis. Basically the regular expression should fetch the operands from a logical expressions.

For example:

INPUT :

1. ((infx.Length != i) && (ch = infx[i++]) != '\0')
2. (infx.Length != i && infx[i++]) != '\0')


OUTPUT :

1. infx.Length != i
ch = infx[i++]) != '\0'

2. infx.Length != i
infx[i++]) != '\0'

Thanks in Advance

推荐答案

老实说,正则表达式是一种不好的方法:正则表达式非常擅长模式匹配,但是他们的语法分析非常差,这就是这个任务所需要的。



有可能匹配括号等等 - 尽管它很复杂 - 但它不能轻易发现这样的事情:

To be honest, a regex is a poor way to do this: regular expressions are very good at pattern matching, but they are very poor are syntactic analysis, which is what this task requires.

It is possible to match brackets and so forth - though it is quite complex - but it can't easily spot things like this:
(ch = infx[i++]) != ')')



我会看看表达式的标记,并将其用于评估。


I would look at tokenising the expression, and using that for your evaluation.


输出中应该有三个部分,第三部分应该是

There should be three parts in the output, the third one should be
!= '\0'



无论如何,正则表达式不适用于所有内容,请尝试以下方法:


Anyway, regular expression is not for everything, try the following approach:

string originalString = @"((infx.Length != i) && (ch = infx[i++]) != '\0')";
Console.WriteLine("The original string is {0}", originalString);
// more separators can be added here
char[] charSeparators = new char[] {'(', '&', ')'};
string[] result = originalString.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
    if (s != " ") //ignore white space
    {
        Console.Write("{0} \n",  s);
    }
}


这篇关于正则表达式需要在括号之间读入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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