如何使用Java中的RegEx模式检查LinkedIn Web链接?

Wil*_*lli 2 java regex url-pattern

谁能帮我吗?我正在使用Java中的URL链接进行验证。我创建了一个正则表达式(我不熟悉)来验证链接,但是我为此感到吃力。

代码如下:

public class TestRegEx {

    public static void main(final String[] args) {

        // List of valid URLs
        List<String> validValues = new ArrayList<>();
        validValues.add("https://www.linkedin.com/sometext");
        validValues
                .add("https://uk.linkedin.com/in/wiliam-ferraciolli-a9a29795");
        validValues.add("https://it.linkedin.com/hp/");
        validValues.add("https://cy.linkedin.com/hp/");
        validValues
                .add("https://www.linkedin.com/profile/view?id=AAIAABQnNlYBIx8EtS5T1RTUbxHQt5Ww&trk=nav_responsive_tab_profile");


        // List on invalid URLs
        List<String> invalidValues = new ArrayList<>();
        invalidValues.add("http://www.linkedin.com/sometext");
        invalidValues.add("http://stackoverflow.com/questions/ask");
        invalidValues.add("google.com");
        invalidValues.add("http://uk.linkedin.com/in/someDodgeAddress");
        invalidValues.add("http://dodge.linkedin.com/in/someDodgeAddress");

        // Pattern
        String regex = "(https://)(.+)(www.)(.+)$";
        Pattern pattern = Pattern.compile(regex);

        for (String s : validValues) {
            Matcher matcher = pattern.matcher(s);
            System.out.println(s + " // " + matcher);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

谁能帮助我创建一个正则表达式来验证以下 前缀: “ https://” 可选前缀: “ uk”。(可以是任何国家或其他国家) 中间: “ linkedin.com/” 后缀: “最多200个字符的任何字符”

问候

Tho*_*oub 6

我会去:

^https:\\/\\/[a-z]{2,3}\\.linkedin\\.com\\/.*$
Run Code Online (Sandbox Code Playgroud)

LiveDemo


^ assert position at start of a line
https: matches the characters https: literally (case insensitive)
\/ matches the character / literally
\/ matches the character / literally
[a-z]{2,3} match a single character present in the list below

    Quantifier: {2,3} Between 2 and 3 times, as many times as possible, giving back as needed [greedy]
    a-z a single character in the range between a and z (case insensitive)

\. matches the character . literally
linkedin matches the characters linkedin literally (case insensitive)
\. matches the character . literally
com matches the characters com literally (case insensitive)
\/ matches the character / literally
.* matches any character (except newline)

    Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

$ assert position at end of a line
Run Code Online (Sandbox Code Playgroud)

  • @Thomas:请参阅[字符类参考](http://www.regular-expressions.info/charclass.html),尤其是*“字符类内部的元字符”部分。 (2认同)