1

私は次の2つのテキストを持っています。

1)v1.0 - 80 s200 + 2013-10-17T05:59:59-0700 1TZY6R5HERP7SJRRYDYV 69.71.202.109 7802 41587 495307 30595 HTTP/1.1 POST /gp/ppd

2)access-1080.2013-10-17-05.us-online-cpp-portlet-live-1d-i-752c3b12.us-east-1.phnew.com.gz

私は彼らからこのデータを取得する必要があります 私が必要とする最初の正規表現から:- .1TZY6R5HERP7SJRRYDYVこれを呼び出しましょうaccessId。これは常に 20 文字で構成され、0 ~ 9 の数字と大文字のアルファベット [AZ] の組み合わせです。

仕方なく使っ[A-Z0-9]{20}てみました。

Pattern p = Pattern.compile([A-Z0-9]{20});  
Matcher m = p.matcher(myString);

また、パターンに一致するJava APIを探しています。一致すると、結果としてパターンが得られます

私が必要とする秒からus-online-cpp-portlet-live-1d-i-752c3b12.us-east-1.phnew.com。私はこれを解読するのに苦労しています。

どんな助けでも役に立ちます。

4

2 に答える 2

3

一致した結果を得るには、次のように呼び出す必要がありますMatcher#find()Matcher#group()

Pattern p = Pattern.compile("[A-Z0-9]{20}");
Matcher m = p.matcher(myString);
String accessId = null;
if (m.find())
   accessId = m.group();
于 2013-10-17T13:48:40.520 に答える
2

コードにはいくつかの問題があります。たとえば、Pattern初期化に二重引用符がありません。

探しているものの例を次に示します。

// text for 1st pattern
String text1 = "v1.0 - 80 s200 + 2013-10-17T05:59:59-0700 1TZY6R5HERP7SJRRYDYV 69.71.202.109 7802 41587 495307 30595 HTTP/1.1 POST /gp/ppd";
// text for 2nd pattern
String text2 = "access-1080.2013-10-17-05.us-online-cpp-portlet-live-1d-i-752c3b12.us-east-1.phnew.com.gz";
// 1st pattern - note that the "word" boundary separators are useless here, 
// but they might come in handy if you had alphanumeric Strings longer than 20 characters
Pattern accessIdPattern = Pattern.compile("\\b[A-Z0-9]{20}\\b");
Matcher m = accessIdPattern.matcher(text1);
while (m.find()) {
    System.out.println(m.group());
}
// this is trickier. I assume for your 2nd pattern you want something delimited on the
// left by a dot and starting with 2 lowercase characters, followed by a hyphen, 
// followed by a number of alnums, followed by ".com"
Pattern otherThingie = Pattern.compile("(?<=\\.)[a-z]{2}-[a-z0-9\\-.]+\\.com");
m = otherThingie.matcher(text2);
while (m.find()) {
    System.out.println(m.group());
}

出力:

1TZY6R5HERP7SJRRYDYV
us-online-cpp-portlet-live-1d-i-752c3b12.us-east-1.phnew.com
于 2013-10-17T13:57:09.313 に答える