英文:
How to extract data using pattern and matcher
问题
嗨,我正在尝试将旧的URL映射到新的URL。例如 -
/oldapp/viewReview.do?action=show_references&bugId=xy12&queueName=OLLD-CodeReviews
映射为 - newapp/review/reference?bugId=xy12&queueName=OLLD-CodeReviews
我该如何使用Pattern和Matcher来匹配模式,并从URL中提取bugId和queueName。请帮忙。
英文:
Hey i am trying to map old URLs to new url. Like -
/oldapp/viewReview.do?action=show_references&bugId=xy12&queueName=OLLD-CodeReviews
to - newapp/review/reference?bugId=xy12&queueName=OLLD-CodeReviews
How i can use Pattern and Matcher to match the pattern and extract bugId and queueName from URL. please help.
答案1
得分: 1
Pattern bugidp = Pattern.compile(".*[?&]bugId=([^&]+).*");
Pattern queuep = Pattern.compile(".*[?&]queueName=([^&]+).*");
Matcher bugidm = bugidp.matcher(url);
Matcher queuem = queuep.matcher(url);
if (bugid.matches() && queuem.matches()) {
String bugid = bugidm.group(1);
String qname = queuem.group(1);
String newrl = String.format("newapp/review/reference?bugId=%s&queueName=%s",
bugid, qname);
} else {
// not found
}
英文:
Any characters followed by ?
or &
followed by the identifier, =
, and the value which cannot contain &
as a group, and then any trailing characters:
Pattern bugidp = Pattern.compile(".*[?&]bugId=([^&]+).*");
Pattern queuep = Pattern.compile(".*[?&]queueName=([^&]+).*");
Matcher bugidm = bugidp.matcher(url);
Matcher queuem = queuep.matcher(url);
if (bugid.matches() && queuem.matches()) {
String bugid = bugidm.group(1);
String qname = queuem.group(1);
String newrl = String.format("newapp/review/reference?bugId=%s&queueName=%s",
bugid, qname);
} else {
// not found
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论