我已经做了一堆搜索,但我很糟糕的正则表达式和我的谷歌福在这种情况下,不强。与错误检查正则表达式
场景:
在推送通知,我们通过包含一个9位数的内容ID的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”
更换'{9}'和'+'(出现一次或多次)。 –
会在内容ID之前绕过标题部分中的任何数字吗? – Psest328
查看https://regex101.com/r/3PamZj/1 –