英文:
URL regex match with and without query strings
问题
我需要一个正则表达式,它可以匹配以下字符串,符合以下条件:
https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760
答案:True
https://example.com/jobs/2273/software-engineer/job
答案:True
https://example.com/jobs/2273/software-engineer/job?mobile=true
答案:False(应包含与#1中的两个键值相同的内容)
简而言之,如果URL以job结尾,正则表达式应该匹配它,就像#2中一样,否则如果它包含一些参数,那么它必须包含mobile=true和width,并且可以接受额外的参数。
英文:
I need a regex which could match the following string under below mentioned condition
https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760
Ans: True
https://example.com/jobs/2273/software-engineer/job
Ans:True
https://example.com/jobs/2273/software-engineer/job?mobile=true
Ans: False (should contain both key values as in #1
In short the regex should match the url if it end upto job as in #2 else if it contains some parameters then it must contain mobile=true and width both must be present and additional parameters are accepted.
答案1
得分: 1
你不需要正则表达式。使用 java.net.URL 类中的方法,即 getPath() 和 getQuery()。
根据你的问题,getPath() 方法返回的字符串应该以 job 结尾,而 getQuery() 方法应该返回 null 或一个包含 mobile= 和 width= 的字符串。
/* 导入 java.net.URL */
URL url = new URL("whatever"); // 用你实际的URL替换 "whatever"
String path = url.getPath();
if (path.endsWith("job")) {
String query = url.getQuery();
if (query == null || (query.contains("mobile=") && query.contains("width="))) {
// 有效
}
}
英文:
You don't need a regular expression. Use the methods in class java.net.URL, namely getPath() and getQuery().
According to your question, the string returned by method getPath() should end with job and method getQuery() should either return null or a string that contains both mobile= and width=
/* import java.net.URL */
URL url = new URL("whatever"); // replace 'whatever' with your actual URL
String path = url.getPath();
if (path.endsWith("job")) {
String query = url.getQuery();
if (query == null || (query.contains("mobile=") && query.contains("width=")) {
// valid
}
}
答案2
得分: 1
使用正则表达式,(?:.*\/job\?(mobile=(?:true|false)&width=\d+))|(?:.*\/job\?(width=\d+&mobile=(?:true|false)))|.*job(?!.)
查看 这个链接 以获取快速演示和解释。
使用Java代码进行演示:
public class Main {
public static void main(String[] args) {
// 测试URL字符串
String[] urls = {
"https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760",
"https://example.com/jobs/2273/software-engineer/job?mobile=false&width=760",
"https://example.com/jobs/2273/software-engineer/job?width=760&mobile=true",
"https://example.com/jobs/2273/software-engineer/job?width=760&mobile=false",
"https://example.com/jobs/2273/software-engineer/job",
"https://example.com/jobs/2273/software-engineer/job?mobile=true"
};
String regex = "(?:.*\\/job\\?(mobile=(?:true|false)&width=\\d+))|(?:.*\\/job\\?(width=\\d+&mobile=(?:true|false)))|.*job(?!.)";
for (String s : urls) {
System.out.println(s + " => " + s.matches(regex));
}
}
}
输出:
https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760 => true
https://example.com/jobs/2273/software-engineer/job?mobile=false&width=760 => true
https://example.com/jobs/2273/software-engineer/job?width=760&mobile=true => true
https://example.com/jobs/2273/software-engineer/job?width=760&mobile=false => true
https://example.com/jobs/2273/software-engineer/job => true
https://example.com/jobs/2273/software-engineer/job?mobile=true => false
英文:
Use the regex, (?:.*\/job\?(mobile=(?:true|false)&width=\d+))|(?:.*\/job\?(width=\d+&mobile=(?:true|false)))|.*job(?!.)
Check this for a quick demo and explanation.
Demo using Java code:
public class Main {
public static void main(String[] args) {
// Test URL strings
String[] urls = { "https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760",
"https://example.com/jobs/2273/software-engineer/job?mobile=false&width=760",
"https://example.com/jobs/2273/software-engineer/job?width=760&mobile=true",
"https://example.com/jobs/2273/software-engineer/job?width=760&mobile=false",
"https://example.com/jobs/2273/software-engineer/job",
"https://example.com/jobs/2273/software-engineer/job?mobile=true" };
String regex = "(?:.*\\/job\\?(mobile=(?:true|false)&width=\\d+))|(?:.*\\/job\\?(width=\\d+&mobile=(?:true|false)))|.*job(?!.)";
for (String s : urls) {
System.out.println(s + " => " + s.matches(regex));
}
}
}
Output:
https://example.com/jobs/2273/software-engineer/job?mobile=true&width=760 => true
https://example.com/jobs/2273/software-engineer/job?mobile=false&width=760 => true
https://example.com/jobs/2273/software-engineer/job?width=760&mobile=true => true
https://example.com/jobs/2273/software-engineer/job?width=760&mobile=false => true
https://example.com/jobs/2273/software-engineer/job => true
https://example.com/jobs/2273/software-engineer/job?mobile=true => false
答案3
得分: 0
尝试这个正则表达式:
.+\/job\b(\?mobile=true\&width=\d+)?$
请检查演示
在Java中,这应该写成类似这样:
String regex = ".+\\/job\\b(\\?mobile=true\\&width=\\d+)?$";
英文:
Try this regex:
.+\/job\b(\?mobile=true\&width=\d+)?$
Please check the demo
In Java this must be written something like this:
String regex = ".+\\/job\\b(\\?mobile=true\\&width=\\d+)?$";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论