使用Regex匹配模式Java

时间:2022-02-15 23:35:03

I have two lists of words,

我有两个单词列表,

A: pit pot spate slaptwo respite,

- 答:坑坑spate slaptwo喘息,,

B: pt Pot peat part

B:pt Pot泥炭部分

I have to create a regex Pattern that returns true for everything in column A and false for everything in column B. I think there is something I'm fundamentally missunderstanding about regex, because everything I come up with ends up with having one extra false where I don't want it. Mainly I can't seen to figure out how to disallow two vowels next to each other.

我必须创建一个正则表达式模式,对于A列中的所有内容都返回true,对于B列中的所有内容都返回false。我认为有一些我从根本上对正则表达式有所了解,因为我想出的一切最终会导致一个额外的错误我不想要它。主要是我无法弄清楚如何禁止彼此相邻的两个元音。

Here's what I have:

这就是我所拥有的:

public static void main(String[] args){
    String[] check = {"pit","spot","spate","slaptwo","respite","pt","Pot","peat","part"};
    Pattern p = Pattern.compile("([psr][^t(ea)][^r])\\w*");

    ArrayList<Matcher> M = new ArrayList<Matcher>();
    for (int i = 0; i < check.length; i++) {
        M.add(p.matcher(check[i]));
    }

    for (int j = 0; j < M.size(); j++){ 
        System.out.println("Return Value:" +check[j] + "\t"+ M.get(j).matches());
    } 
}

I understand now that (ea) is not being read as one thing so it causes respite to be false when I don't want it to, everything else returns the correct value. As I said before I need to know how to disallow two vowels being next to each other. Or if I am just missing something fundamental here?

我现在明白了(ea)并没有被视为一件事,所以当我不想要它时,它会导致喘息,其他一切都会返回正确的值。正如我之前所说,我需要知道如何禁止两个元音彼此相邻。或者,如果我只是遗漏了一些基本的东西?

1 个解决方案

#1


1  

You cannot use grouping constructs inside of a character class.

您不能在字符类中使用分组构造。

To disallow "ea" you can use Negative Lookahead here.

要禁止“ea”,您可以在此处使用“否定前瞻”。

[psr](?!ea)[^t][^r]\\w*

Live Demo

现场演示

#1


1  

You cannot use grouping constructs inside of a character class.

您不能在字符类中使用分组构造。

To disallow "ea" you can use Negative Lookahead here.

要禁止“ea”,您可以在此处使用“否定前瞻”。

[psr](?!ea)[^t][^r]\\w*

Live Demo

现场演示