URL正则匹配,包括查询字符串和不包括查询字符串。

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

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=truewidth,并且可以接受额外的参数。

英文:

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+)?$";

huangapple
  • 本文由 发表于 2020年8月4日 00:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233765.html
匿名

发表评论

匿名网友

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

确定