简体   繁体   English

我正在尝试质疑正则表达式匹配

[英]I'm trying to question a regular expression match

'[loc]' must be included at the beginning and any character after it does not matter. '[loc]' 必须包含在开头,后面的任何字符都没有关系。 How do I express it in a regular expression?我如何用正则表达式表达它? ex)前任)

>>> [loc]korea
true
>>> [loc] 
false // (One or more strings must follow [loc])
>>> [lll]seoul
false
>>> busan 
false

I used Java and I can't really think of it.我用的是Java,实在想不出来。 Ask for help, brothers求大哥帮忙

            String pattern = "\\[(loc)\\]"; 
            String val = "[loc]seoul"; 
            String val2 = "[loc]";
            String val3 = "seoul";
        
            boolean regex = Pattern.matches(pattern, val);
            System.out.println(regex);
            boolean regex2 = Pattern.matches(pattern, val2);
            System.out.println(regex2);
            boolean regex3 = Pattern.matches(pattern, val3);
            System.out.println(regex3);

I understand you have some string, a keyword, that must appear at the start of a string.我知道你有一些字符串,一个关键字,必须出现在字符串的开头。 In that case, you need to Pattern.quote to escape any special chars that need escaping and use the .+ pattern to match one or more chars .在这种情况下,您需要Pattern.quote转义任何需要 escaping 的特殊字符,并使用.+模式匹配一个或多个字符 (You may also consider the .* pattern to match any zero or more chars if your first question sentence is correct and you do not care what is after [loc] .) To also match line breaks, that . (如果您的第一个问题句子是正确的并且您不关心[loc]之后的内容,您也可以考虑.*模式来匹配任何零个或多个字符。)为了也匹配换行符,那. does not match by default, you need pass the Pattern.DOTALL option, or prepend your pattern with (?s) inline modifier.默认情况下不匹配,您需要传递Pattern.DOTALL选项,或在您的模式前添加(?s)内联修饰符。

The solution will look like解决方案看起来像

List<String> vals = Arrays.asList(
    "[loc]seoul", "[loc]", "seoul"
);
String keyword = "[loc]";
String pattern = Pattern.quote(keyword) + ".+";
Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
for (String val : vals) {
    System.out.println(val + ": \"" + p.matcher(val).matches() + "\"");
}

See the Java demo .请参阅Java 演示 Output: Output:

[loc]seoul: "true"
[loc]: "false"
seoul: "false"

Its easy.这简单。 The regex expression should be like loc\w+正则表达式应该像loc\w+
For java, you can parse it as loc\\w+对于java,可以解析为loc\\w+


'[loc]' must be included at the beginning and any character after it does not matter. '[loc]' 必须包含在开头,后面的任何字符都没有关系。 How do I express it in a regular expression?如何用正则表达式表达它? ex)前任)

>>> [loc]korea
true
>>> [loc] 
false // (One or more strings must follow [loc])
>>> [lll]seoul
false
>>> busan 
false

I used Java and I can't really think of it.我用过 Java 实在想不出来。 Ask for help, brothers求兄弟们帮忙

            String pattern = "\\[(loc)\\]"; 
            String val = "[loc]seoul"; 
            String val2 = "[loc]";
            String val3 = "seoul";
        
            boolean regex = Pattern.matches(pattern, val);
            System.out.println(regex);
            boolean regex2 = Pattern.matches(pattern, val2);
            System.out.println(regex2);
            boolean regex3 = Pattern.matches(pattern, val3);
            System.out.println(regex3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM