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

问题描述 投票:2回答:2

有人可以帮我吗?我正在使用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);
        }

    }
}

有人可以帮助我创建正则表达式来验证以下内容吗?前缀:“ https://”可选前缀:“英国”。 (可以是空无一物或其他国家)中:“ linkedin.com/”后缀:“最多200个字符的任何字符”

问候

java regex url-pattern
2个回答
7
投票
^https:\\/\\/[a-z]{2,3}\\.linkedin\\.com\\/.*$

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


0
投票
http(s)?:\/\/([\w]+\.)?linkedin\.com\/in\/[A-z0-9_-]+\/?
© www.soinside.com 2019 - 2024. All rights reserved.