简体   繁体   English

正则表达式不允许 a 和 c 彼此相邻

[英]Regular expression not allowing a and c to be next to each other

I'm trying to write a regular expression which doesn't allow 'a' and 'c' to be next to each other in any combination of "abc", the combinations might be "a", "b", "c", "acb", "abac", here "abac" must be ignored because it contains "a" and "c" next to each other, I've written a regular expression which is doing half the job correct and the other half incorrect, it's basically ignoring a, bcb, bcc and others which are not supposed to be ignored.我正在尝试编写一个正则表达式,它不允许“a”和“c”在“abc”的任何组合中彼此相邻,这些组合可能是“a”、“b”、“c” , "acb", "abac", 这里的 "abac" 必须被忽略,因为它包含彼此相邻的 "a" 和 "c",我写了一个正则表达式,它完成了一半的工作,另一半不正确, 它基本上忽略了 a, bcb, bcc 和其他不应该被忽略的。

Here's the regular expression:这是正则表达式:

^(a?b)*c?$

Here's the output I'm getting:这是我得到的 output:

[a, b, c, ba, ca, ab, cb, ac, bc, baa, caa, aba, cba, aca, bca, 
bab, cab, abb, cbb, acb, bcb, bac, cac, abc, cbc, acc, bcc]
b 
c 
ab 
bc 
bab 
abb 
abc 

Could someone please tell me what I'm doing wrong?有人可以告诉我我做错了什么吗?

Your expression ignores several cases:您的表达忽略了几种情况:

  • anything with more than one c is ignored超过一个c的任何内容都将被忽略
  • anything with a or b coming after c is ignored (that means if there is a c that has to be the last character)c之后带有ab的任何内容都将被忽略(这意味着如果有一个c必须是最后一个字符)
  • anything containing an a is ignored if it doesn't contain also a b after that a如果在 a 之后不包含b ,则忽略任何包含a a
  • each a must be followed by a b每个a后面必须跟一个b
  • also your grouping is probably not really the form you need.您的分组也可能不是您真正需要的形式。 You should use (?:X) for a non capturing group.您应该将(?:X)用于非捕获组。

I would suggest a regex like我建议像这样的正则表达式

^(?:(?:a(?!c))?b?c?)+$

This matches all a s not followed by a c and also all b s and c s - and needs at least one occurence so that empty strings are not matched.这匹配所有a后面没有跟c以及所有bc s - 并且需要至少出现一次以便不匹配空字符串。

You can play with it and get detailed explanations at https://regex101.com/r/DoyUPG/1您可以在https://regex101.com/r/DoyUPG/1上玩并获得详细的解释

Here is a much simpler straightforward regex.这是一个更简单直接的正则表达式。 Rather than thinking to exclude the pattern, you can also match the pattern and ignore them like following example:与其考虑排除模式,还可以匹配模式并忽略它们,如下例所示:

String[] str = {
    "a", "b", "c", "ba", "ca", "ab", "cb", "ac", "bc", "baa",
    "caa", "aba", "cba", "aca", "bca", "bab", "cab", "abb",
    "cbb", "acb", "bcb", "bac", "cac", "abc", "cbc", "acc", "bcc"
};
        
for(int i=0; i<str.length; ++i) {
    if(str[i].matches("ac.?|.?ac|ca.?|.?ca")) {
        System.out.println("MATCH: " + str[i]);
    } else {
        System.out.println(str[i]);
    }
}

This makes the following output:这使得以下 output:

a
b
c
ba
MATCH: ca
ab
cb
MATCH: ac
bc
baa
MATCH: caa
aba
cba
MATCH: aca
MATCH: bca
bab
MATCH: cab
abb
cbb
MATCH: acb
bcb
MATCH: bac
MATCH: cac
abc
cbc
MATCH: acc
bcc

In spite of your provided data, you said "the combinations might be "a", "b", "c", "acb", "abac", here "abac" which indicates they could be more than just three letters. Rather than use a regex I recommend String.contains .尽管您提供了数据,但您说"the combinations might be "a", "b", "c", "acb", "abac", here "abac"表示它们可能不仅仅是三个字母。而是比使用regex我推荐String.contains

String[] str = { "a", "b", "c", "babbacb", "ca", "ab", "cb",
        "aeseac", "bc", "baa", "caa", "aba", "cba", "aca",
        "bca", "bab", "cab", "abb", "cbbabcda", "acb",
        "bcbacbae", "bacadbac", "adecdcac", "abc", "cbc",
        "acc", "adbbcc", "abac" };
        
for (String s : str) {
    if (!(s.contains("ac") | s.contains("ca"))) {
        System.out.println(s);
    }
}

prints印刷

a
b
c
ab
cb
bc
baa
aba
cba
bab
abb
cbbabcda
abc
cbc
adbbcc

But if you want to use a regex then simply check for those strings that matches string that is composed of at least one ac or ca .但是,如果您想使用regex ,则只需检查那些与至少由一个acca组成的字符串相匹配的字符串。

String regex = ".*((ac)|(ca)).*";
for (String s : str) {
    if (!s.matches(regex)) {
        System.out.println(s);
    }
}

prints the same as above.打印与上面相同。

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

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