2012-04-18 148 views
0

我想实现模式匹配的形式正则表达式匹配不能正常工作

(a + b)(c-或*或/ d)............. 任何次数。

我使用以下模式,但它不能递归工作。 它只是读第一组。

Pattern pattern; 
String regex="(([0-9]*)([+,-,/,*])([0-9]*)*)"; 
pattern=Pattern.compile(regex); 
Matcher match = pattern.matcher(userInput); 
+0

你在哪个上下文中使用这个。可以给一个示例文本与正则表达式进行测试? – 2012-04-18 03:45:45

+1

ok 15-9 * 12/5 + 9 * 4我需要解析这个作为用户输入并计算这个表达式 – 2012-04-18 03:50:46

+0

计算或检查?正则表达式用于匹配,不检查计算。 – 2012-04-18 04:03:06

回答

0

你需要匹配的排序序列的正则表达式是这样的:

\s*-?\d+(?:\s*[-+/*]\s*-?\d+)+\s*

让我们打破下来到它的组成部分!

\s*   # Optional space 
-?    # Optional minus sign 
\d+   # Mandatory digits 
(?:   # Start sub-regex 
    \s*   # Optional space 
    [-+*/]  # Mandatory single arithmetic operator 
    \s*   # Optional space 
    -?   # Optional minus sign 
    \d+   # Mandatory digits 
)+    # End sub-regex: want one or more matches of it 
\s*   # Optional space 

(如果你不想匹配空格,删除所有这些\s*,并知道它会惊讶的用户不少。)

现在,编码上述作为Java的一个字符串文字时(编译前),你需要小心逃避每个\字符在它:

String regex="\\s*-?\\d+(?:\\s*[-+/*]\\s*-?\\d+)+\\s*"; 

另外要注意的是这个不是将正则表达式分解为多个部分,以供Java解析和构建表达式评估树;它只是(与您的其他代码)匹配整个字符串或不。 (即使插入括号也没有多大帮助;当放入某种形式的重复时,它们只报告它们匹配的字符串中的第一位)。正确执行此操作的最简单方法是使用解析器生成器Antlr(它也可以让你做像加括号的子表达式,管理运营商的优先级等等)

+0

谢谢。你的正则表达式对我来说工作得很好。为了解析我打算使用Stack实现。再次感谢。 – 2012-04-18 06:55:12

+0

@Rohit:“堆栈实现”很像LL解析器,这就是ANTLR是一个构建工具。 (像这样的名字,你会认为它会让LR解析器,但它不;但又错过了一个很酷的名字的机会......) – 2012-04-18 16:30:17

0

你需要这样的

[0-9]+-[0-9]+[\/*-+][0-9]+[\/*-+][0-9]+[\/*-+][0-9]+[\/*-+][0-9]+ 

你必须整个表达式匹配的表达式。您无法匹配部分表达式并进行第二次搜索,因为重复了该模式。

注意:在ruby \中是/字符的隐藏序列,所以你可以在C#中忽略它,或者用另一个字符替换它。

Demo

-1

你的表达犯规逃脱特殊字符,例如+,(,)

试试这个

/\(\d+[\+|-|\/|\*]\d+)\G?/ 

\ G是一遍又一遍

整个模式?意味着以前的事情是可选

我改变你的[0-9] *为\ d +我认为这是比较正确的

我改变了你,给|

+0

Nah,'\ G'不能像你的描述那样工作。它匹配最后一场比赛的边界 - 这意味着它声明那个位置是最后比赛结束的地方。 – nhahtdh 2014-01-11 18:19:09

0

图案

<!-- 
\((\d|[\+\-\/\\\*\^%!]+|(or|and) *)+\) 

Options:^and $ match at line breaks 

Match the character “(” literally «\(» 
Match the regular expression below and capture its match into backreference number 1 «(\d|[\+\-\/\\\*\^%!]+|(or|and) *)+» 
    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
    Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «+» 
    Match either the regular expression below (attempting the next alternative only if this one fails) «\d» 
     Match a single digit 0..9 «\d» 
    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[\+\-\/\\\*\^%!]+» 
     Match a single character present in the list below «[\+\-\/\\\*\^%!]+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     A + character «\+» 
     A - character «\-» 
     A/character «\/» 
     A \ character «\\» 
     A * character «\*» 
     A^character «\^» 
     One of the characters “%!” «%!» 
    Or match regular expression number 3 below (the entire group fails if this one fails to match) «(or|and) *» 
     Match the regular expression below and capture its match into backreference number 2 «(or|and)» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «or» 
      Match the characters “or” literally «or» 
     Or match regular expression number 2 below (the entire group fails if this one fails to match) «and» 
      Match the characters “and” literally «and» 
     Match the character “ ” literally « *» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
Match the character “)” literally «\)» 
--> 

该计算算法
对于必须使用堆栈解析和处理输入字符串。访问here的概念。

问候
Cylian