0

パターンを持つ任意の文字列に一致させたい

{"id":"362237- 
any number of characters followed by
"http//:www.abc.com" 
any number of characters followed by
"id":"364121-
any number of characters followed by
"http://www.efg.com"

上記のパターンを下の文字列に一致させたい。

[{"id":"362237-13","http//:www.abc.com"},{"id":"364075-13","http://www.xyz.com"},{"id":"364121-13","http://www.efg.com"}]

コード:

String pttrn=".*{\"id\":"362237-.*\"http//:www.abc.com\".*\"id\":"364121-.*\"http://www.efg.com\".*";

String mtchr="[{\"id\":\"362237-13\",\"http//:www.abc.com\"},{\"id":\"364075-13\",\"http://www.xyz.com\"},{\"id\":\"364121-13\",\"http://www.efg.com\"}]";


        boolean b = Pattern.matches(pttrn, mtchr);
        System.out.println("b is !!" + b);

bがtrueになることを期待していましたが、falseが返されます。正規表現が間違っています。

修正方法を教えてください。

ありがとう

4

1 に答える 1

0

バックスラッシュを使用して、中かっこを正規表現エンジンにエスケープする必要があります。...バックスラッシュを別のバックスラッシュで Java にエスケープする必要があります。

String pttrn=".*\\{\"id\":\"362237-.*\"http//:www.abc.com\".*\"id\":\"364121-.*\"http://www.efg.com\".*";
String mtchr="[{\"id\":\"362237-13\",\"http//:www.abc.com\"},{\"id\":\"364075-13\",\"http://www.xyz.com\"},{\"id\":\"364121-13\",\"http://www.efg.com\"}]";
boolean b = Pattern.matches(pttrn, mtchr);
System.out.println("b is !!" + b);
于 2013-09-25T06:14:53.073 に答える