有人可以帮我写一个正则表达式吗?

时间:2020-11-29 01:31:34

I'm making a search function for my app and I need a good regular expression. Right now, when the user searches "RAC" (it's a band name), it returns all of my posts that have the letters "rac" in the title. This, however, is a bit broken and can only be fixed by a regular expression. I will provide some examples and say whether or not they should be included.

我正在为我的应用程序创建搜索功能,我需要一个很好的正则表达式。现在,当用户搜索“RAC”(它是一个乐队名称)时,它会返回标题中包含字母“rac”的所有帖子。但是,这有点破碎,只能通过正则表达式修复。我将提供一些例子并说明是否应该包括它们。

  1. Sir Sly - Miracle : DON'T INCLUDE
  2. Sir Sly - Miracle:不要包括在内
  3. RAC - Let Go : INCLUDE
  4. RAC - 放手:包括
  5. Amtrac - Walkin' : DON'T INCLUDE
  6. Amtrac - Walkin':不要包括在内
  7. Bastille - Laura Palmer (RAC Remix) : INCLUDE
  8. 巴士底狱 - 劳拉帕尔默(RAC Remix):包括
  9. Cold War Kids - Miracle Mile : DON'T INCLUDE
  10. 冷战孩子 - 奇迹英里:不要包括
  11. MNDR - Feed Me Diamons (ft. RAC) : INCLUDE
  12. MNDR - Feed Me Diamons(英尺RAC):包括

Does anyone have a good regular expression that could help me out? P.S. you can test your regular expression here.

有没有人有一个很好的正则表达可以帮助我?附:你可以在这里测试你的正则表达式。

3 个解决方案

#1


3  

Edit: this is a solution for Java - if not the intended language, please refine your tags

编辑:这是Java的解决方案 - 如果不是预期的语言,请优化您的标签

You can use word boundaries for this, and a case-sensitive Pattern, as such:

您可以为此使用单词边界,以及区分大小写的Pattern,如下所示:

String[] inputs = {"Sir Sly - Miracle",// : DON'T INCLUDE
        "RAC - Let Go",// : INCLUDE
        "Amtrac - Walkin'",// : DON'T INCLUDE
        "Bastille - Laura Palmer (RAC Remix)",// : INCLUDE
        "Cold War Kids - Miracle Mile",// : DON'T INCLUDE
        "MNDR - Feed Me Diamons (ft. RAC)"// : INCLUDE
        };
Pattern p = Pattern.compile("\\bRAC\\b");
for (String input: inputs) {
    Matcher m = p.matcher(input);
    System.out.printf("%s --> found ? %b%n", input, m.find());
}

Output

产量

Sir Sly - Miracle --> found ? false
RAC - Let Go --> found ? true
Amtrac - Walkin' --> found ? false
Bastille - Laura Palmer (RAC Remix) --> found ? true
Cold War Kids - Miracle Mile --> found ? false
MNDR - Feed Me Diamons (ft. RAC) --> found ? true

#2


0  

There's many tools that can help you instead of rolling your own solution. Have you considered Lucene

有许多工具可以帮助您而不是滚动您自己的解决方案。你考虑过Lucene吗?

#3


-1  

Similar solution... but more efficient
in order to avoid to allocate useless objects

类似的解决方案......但更有效率,以避免分配无用的对象

String[] bands =  {"Sir Sly - Miracle",// : DON'T INCLUDE
                   "RAC - Let Go",// : INCLUDE
                   "Amtrac - Walkin'",// : DON'T INCLUDE
                   "Bastille - Laura Palmer (RAC Remix)",// : INCLUDE
                   "Cold War Kids - Miracle Mile",// : DON'T INCLUDE
                   "MNDR - Feed Me Diamons (ft. RAC)",// : INCLUDE
};

for(String s : bands){
    if(Pattern.matches(".*(RAC).*", s))
            System.out.printf("%s\n",s);
}

#1


3  

Edit: this is a solution for Java - if not the intended language, please refine your tags

编辑:这是Java的解决方案 - 如果不是预期的语言,请优化您的标签

You can use word boundaries for this, and a case-sensitive Pattern, as such:

您可以为此使用单词边界,以及区分大小写的Pattern,如下所示:

String[] inputs = {"Sir Sly - Miracle",// : DON'T INCLUDE
        "RAC - Let Go",// : INCLUDE
        "Amtrac - Walkin'",// : DON'T INCLUDE
        "Bastille - Laura Palmer (RAC Remix)",// : INCLUDE
        "Cold War Kids - Miracle Mile",// : DON'T INCLUDE
        "MNDR - Feed Me Diamons (ft. RAC)"// : INCLUDE
        };
Pattern p = Pattern.compile("\\bRAC\\b");
for (String input: inputs) {
    Matcher m = p.matcher(input);
    System.out.printf("%s --> found ? %b%n", input, m.find());
}

Output

产量

Sir Sly - Miracle --> found ? false
RAC - Let Go --> found ? true
Amtrac - Walkin' --> found ? false
Bastille - Laura Palmer (RAC Remix) --> found ? true
Cold War Kids - Miracle Mile --> found ? false
MNDR - Feed Me Diamons (ft. RAC) --> found ? true

#2


0  

There's many tools that can help you instead of rolling your own solution. Have you considered Lucene

有许多工具可以帮助您而不是滚动您自己的解决方案。你考虑过Lucene吗?

#3


-1  

Similar solution... but more efficient
in order to avoid to allocate useless objects

类似的解决方案......但更有效率,以避免分配无用的对象

String[] bands =  {"Sir Sly - Miracle",// : DON'T INCLUDE
                   "RAC - Let Go",// : INCLUDE
                   "Amtrac - Walkin'",// : DON'T INCLUDE
                   "Bastille - Laura Palmer (RAC Remix)",// : INCLUDE
                   "Cold War Kids - Miracle Mile",// : DON'T INCLUDE
                   "MNDR - Feed Me Diamons (ft. RAC)",// : INCLUDE
};

for(String s : bands){
    if(Pattern.matches(".*(RAC).*", s))
            System.out.printf("%s\n",s);
}