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

标签 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);
        }

    }
}


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

问候

最佳答案

我会去:

^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

关于java - 如何在Java中使用RegEx模式检查LinkedIn Web链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33760452/

相关文章:

java - 浮点模数混淆

java - Hadoop Java 字数统计调整不起作用 - 尝试对所有内容进行求和

regex - 正则表达式:- String can contain any characters but should not be empty

Django:使用 gettext 进行 URL 模式转换

servlets - 为 Servlet Filter 提供多个 URL 模式

java - 如何运行 HDFS 测试类?

java - 使用正则表达式过滤文本中具有某些特定条件的电子邮件地址

regex - 正则表达式关于如何指定输入的混淆

java - bean 属性的类型如何为空?