0
Enter your regex: a?
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "a" starting at index 5 and ending at index 6.
I found the text "a" starting at index 6 and ending at index 7.
I found the text "a" starting at index 7 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.

Enter your regex: a*
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.

Enter your regex: a+
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.  



上記の例は、Java Documentation数量詞からのものです。
上記の例から私が理解していることから、貪欲な数量詞がどのように機能するかを次に示します。

?と *

言及されたキャラクターの不在の存在を探します。それらの前の文字が見つからない場合は、その場所を長さゼロの一致としてマークします。
*グループに行く間、文字ごとの一致に?行きます。

+

+正規表現のグループマッチングによってグループ化されます。長さゼロの一致は行われません。

私の質問は次のとおり
です。数量詞の理解は正しいですか、それとも何かを台無しにしていますか?
また、Java正規表現の頭脳に優しいガイドはありますか(Googleには初心者を対象としたガイドはありません)?

4

3 に答える 3

1

はい、あなたの理解は正しいです。

ガイドについては、正規表現を探してください。関数呼び出しとそうでないものを除いて、Java に固有のものは何もありません。たとえば、1 秒の検索でhttp://www.aivosto.com/vbtips/regex.htmlにアクセスできます

編集: Explosion Pillsは、優れた情報源であるhttp://www.regular-expressions.info/を指しています。Java 固有のセクションがありますが、もちろん、パターン自体は標準の正規表現構文のままです。

于 2013-02-18T01:51:28.797 に答える
1
  • 文字ごとの一致に進みますが、?グループに行きます。

*0 個以上に一致します。 ?0 または 1 に一致します。

これらはいずれもグループやキャラクターとは何の関係もありません。java.util.regex.Matcher.group正規表現の用語での「グループ」または「キャプチャ グループ」は、メソッドによって個別に抽出できる括弧で囲まれた要素です。

(foo)*"""foo"などと一致"foofoo"するため、明らかに本文は 1 文字である必要はありません。

(foo)?と一致しますが、一致"""foo"ません"foofoo"

  • 正規表現のグループごとのマッチングに進みます。長さがゼロの一致は作成しません。

No. +は body に 1 回以上一致するため、x*と同等(?:x+)?です。オペレーターは、ゼロ長の+一致を問題なく実行できます。 ()+空の文字列に一致します。

于 2013-02-18T02:19:12.610 に答える
0

私の理解では、?その文字が 0 回または 1 回出現した場合に一致*し、0 回を含む複数回+出現した場合に一致し、1 回以上出現した場合に一致します。

于 2013-02-18T01:54:29.960 に答える