我想在表单中实现模式匹配
(a+b)(c-or*or/d)....... 任意次数。
我使用以下模式,但它不能递归工作。它只是阅读第一组。
Pattern pattern;
String regex="(([0-9]*)([+,-,/,*])([0-9]*)*)";
pattern=Pattern.compile(regex);
Matcher match = pattern.matcher(userInput);
您将需要这样的表达式
[0-9]+-[0-9]+[\/*-+][0-9]+[\/*-+][0-9]+[\/*-+][0-9]+[\/*-+][0-9]+
你必须匹配整个表达式。您无法匹配表达式的一部分并进行第二次搜索,因为该模式重复。
注意:在 ruby 中 \ 是 / 字符的 excape 序列,因此您可以在 C# 中省略它或将其替换为另一个字符。
图案
<!--
\((\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 «\)»
-->
计算算法
要解析和处理输入字符串,您必须使用堆栈。访问这里了解这个概念。
问候
西里安
您需要匹配这种序列的正则表达式是这样的:
\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 文字时(编译之前),您需要小心转义其中的每个\
字符:
String regex="\\s*-?\\d+(?:\\s*[-+/*]\\s*-?\\d+)+\\s*";
要注意的另一件事是,这不会将正则表达式拆分为 Java 解析和构建表达式评估树的部分;它只是(与您的其余代码一起)与整个字符串匹配或不匹配。(即使放入捕获括号也无济于事;当放入某种形式的重复中时,它们只会报告匹配的字符串中的第一个位置。)正确执行此操作的最简单方法是使用解析器生成器,例如Antlr(它还可以让您执行带括号的子表达式、管理运算符优先级等操作)
您的表情不会转义特殊字符,例如 +,(,)
尝试这个
/\(\d+[\+|-|\/|\*]\d+)\G?/
\G 又是整个模式
? 表示前面的事情是可选的
我将您的 [0-9]* 更改为 \d+ 我认为更正确
我把你的 , 改成了 |