2012-04-18 14 views
0

パターンマッチングをフォーム内で実装したい正規表現のパターンマッチングが再帰的に機能しない

(a + b)(cまたは*または/ d)............. 。 は何回でも入力できます。

次のパターンを使用しますが、再帰的に動作しません。 最初のグループを読むだけです。

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

の文脈で使用しています。正規表現でテストするサンプルテキストを与えることができますか? –

+1

ok 15-9 * 12/5 + 9 * 4これをユーザー入力として解析し、この式を計算する必要があります –

+0

計算またはチェック?正規表現はマッチングに使用され、計算はチェックされません。 –

答えて

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

ありがとう。あなたの正規表現は私のためにうまく動作します。私はスタック実装を使用することを計画しています。再度、感謝します。 –

+0

@Rohit:「スタック実装」はLLパーサーによく似ていて、ANTLRはビルドのためのツールです。 (名前のようにLRパーサを作ると思うかもしれませんが、そうではありませんが、クールな名前の層のためのもう一つの逃した機会です...) –

0

あなたは、あなたが全体の表現に一致する必要があり、この

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

のような表現が必要になります。式の一部を一致させることはできず、パターンが繰り返されるため、2回目の検索を行います。

注:ルビ\は、/キャラクタのエクステンションシーケンスなので、C#で省略することも、別のキャラクタで置き換えることもできます。

Demo

-1

あなたの式は+などの特殊文字をエスケープdoesntの、(、)

は、このG \

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

が再び

オーバーパターン全体でみては? |前の事は私は私がに、あなたを変更し、より正確な

だと思うのD +を\に* [0-9]あなたを変更し、オプションの

であることを意味します

+0

ナー、あなたの説明のように '\ G'は動作しません。最後の一致の境界に一致します。つまり、最後の一致が終了する位置を指定します。 – nhahtdh

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

関連する問題