如何使用模式和匹配器提取数据

huangapple go评论86阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2020年5月5日 22:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定