0

I'm trying to get a similar result \ has in Java String literals. If there are two of them, it's a \, otherwise it "escapes" whatever follows. So if there is a delimiter that follows a single release char, it doesn't count. But two release chars resolve to a release char literal, so then the following delimiter should be considered a delimiter. So, if an odd number of release chars precede a delimiter, it's ignored. For 0 or an even number it's a delimiter. So, in the code example below:

?: <- : is not a delimiter
??: <- : is a delimiter
???: <- : is not a delimiter
????: <- : is a delimiter

Here's sample code showing what doesn't work.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestPattern
{
    public static void main(final String[] args)
    {
        final Matcher m = Pattern.compile("(\\?\\?)*[^\\?]\\:").matcher("a??:b:c");
        m.find(0);
        System.out.println(m.end());
    }
}
4

4 に答える 4

0

それが機能する正規表現を逆にするだけのようです。「一致しない ?」を入れる 最初に、次に「偶数の?」がうまくいくようです:

[^?](\\?\\?)*:

于 2013-06-17T18:34:35.970 に答える
0

これ*は、そのグループがゼロになる可能性があることを意味します。そのため、キャプチャ グループは空の文字列にすることができます。文字クラス内の特殊文字ではないため、 で[^\\?]はない任意の文字にすることができます。は無視されます。??\

したがって、b:(前に空の文字列がある) 一致し、2 番目のコロンが最後 (この場合は最初) に一致します。

私はあなたが単に欲しいと思います"(\\?\\?)*\\?:"

于 2013-06-13T19:41:36.707 に答える
0

あなたの正規表現は次のことを意味します:

0 個以上の「??」

(\\?\\?)*

'?' が続きません。

[^\\?]

で終わります ':'

\\:

したがって、最後の一致は最後のコロンです。そのため、結果のオフセットは 6 です。

次のように変更できます。

final Matcher m = Pattern.compile("((\\?){2})+").matcher("a??:b:????:c");
while (m.find()){
    //outputs 1 and 6, places 
    //you would have to start
    //scaping...
    System.out.println(m.start()); 
}
于 2013-06-13T19:37:45.530 に答える