私は検索の束をやったことがありますが、私は正規表現と私のgoogle-fuはこの例では強くないとしてひどいです。エラーチェック付き正規表現
シナリオ:プッシュ通知で
、我々は9桁のコンテンツIDが含まれているURLを可決しています。
例URL:http://www.something.com/foo/bar/Some-title-Goes-here-123456789.html
(123456789がこのシナリオでは、コンテンツIDである)コンテンツIDを解析する
現在の正規表現:
public String getContentIdFromPathAndQueryString(String path, String queryString) {
String contentId = null;
if (StringUtils.isNonEmpty(path)) {
Pattern p = Pattern.compile("([\\d]{9})(?=.html)");
Matcher m = p.matcher(path);
if (m.find()) {
contentId = m.group();
} else if (StringUtils.isNonEmpty(queryString)) {
p = Pattern.compile("(?:contentId=)([\\d]{9})(?=.html)");
m = p.matcher(queryString);
if (m.find()) {
contentId = m.group();
}
}
}
Log.d(LOG_TAG, "Content id " + (contentId == null ? "not found" : (" found - " + contentId)));
if (StringUtils.isEmpty(contentId)) {
Answers.getInstance().logCustom(new CustomEvent("eid_url")
.putCustomAttribute("contentId", "empty")
.putCustomAttribute("path", path)
.putCustomAttribute("query", queryString));
}
return contentId;
}
問題: これは仕事をしていませんが、特定のエラーがありますシナリオを説明する必要があります。
プッシュを作成する人は間違った長さのコンテンツIDを入れることがあり、それに関係なくそれを取得する必要があるため、任意の桁数にすることができます。タイトルには桁違いの数字も含まれます。コンテンツIDには必ず「.html」が続きます。
ように見えることができます。 –
は、コンテンツIDの前にタイトル部分の数字をバイパスしますか? – Psest328
https://regex101.com/r/3PamZj/1を参照 –