java 使用正则表达式查找所有包含 3 个字母的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33017274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:05:23  来源:igfitidea点击:

Find all words with 3 letters with regex

javaregexstringparsing

提问by user1170330

I'm trying to find all words with 3 letters in a string.
So in this list

我试图在一个字符串中找到所有包含 3 个字母的单词。
所以在这个列表中

cat monkey dog mouse

I only want

我只想要

cat dog

This is my expression:

这是我的表达:

^[a-zA-Z]{3}$

I tested it with different online regex tester, but none of them matched my expression.

我用不同的在线正则表达式测试器对其进行了测试,但没有一个与我的表达式匹配。

回答by anubhava

You should use your match with word boundaries instead of anchors:

您应该将匹配与单词边界而不是锚点一起使用:

\b[a-zA-Z]{3}\b

RegEx Demo

正则表达式演示

When you use:

当您使用:

^[a-zA-Z]{3}$

It means you want to match a line with exact 3 letters.

这意味着您要匹配具有精确 3 个字母的行。

回答by maioman

you can use .instead of [a-zA-Z]if you want to match any character (also numbers):

如果您想匹配任何字符(也包括数字),您可以使用.代替[a-zA-Z]

 \b.{3}\b

回答by Ameya Pandilwar

  1. To match all words with 3 letters in a string, the pattern needs to be "\b[a-zA-Z]{3}\b"

  2. The next step would be to compile your pattern.

    Pattern pattern = Pattern.compile("\b[a-zA-Z]{3}\b");
    
  3. Use a matcher and use the find() and group() methods to print the occurrences

    for (String word : sentence) {
        Matcher matcher = pattern.matcher(word);
        while(matcher.find()) {
            System.out.println(matcher.group());
        }
    }
    
  4. Your program should look something like -

    public static void main(String[] args) {
        List<String> sentence = new ArrayList<String>();
        sentence.add("cat");
        sentence.add("monkey");
        sentence.add("dog");
        sentence.add("mouse");
    
        Pattern pattern = Pattern.compile("\b[a-zA-Z]{3}\b");
    
        for (String word : sentence) {
            Matcher matcher = pattern.matcher(word);
            while(matcher.find()) {
                System.out.println(matcher.group());
            }
        }
    }
    
  1. 要匹配字符串中包含 3 个字母的所有单词,模式需要为"\b[a-zA-Z]{3}\b"

  2. 下一步是编译您的模式。

    Pattern pattern = Pattern.compile("\b[a-zA-Z]{3}\b");
    
  3. 使用匹配器并使用 find() 和 group() 方法打印出现的次数

    for (String word : sentence) {
        Matcher matcher = pattern.matcher(word);
        while(matcher.find()) {
            System.out.println(matcher.group());
        }
    }
    
  4. 你的程序应该看起来像 -

    public static void main(String[] args) {
        List<String> sentence = new ArrayList<String>();
        sentence.add("cat");
        sentence.add("monkey");
        sentence.add("dog");
        sentence.add("mouse");
    
        Pattern pattern = Pattern.compile("\b[a-zA-Z]{3}\b");
    
        for (String word : sentence) {
            Matcher matcher = pattern.matcher(word);
            while(matcher.find()) {
                System.out.println(matcher.group());
            }
        }
    }