Regular Expressions with Java Patterns

2

I need to do a college exercise that is as follows: Validate with regular expressions any word that contains exactly two 'a' characters and two 'b' characters or more. I made the following expression in class Pattern :

Pattern pattern = Pattern.compile("a{2}b{2,}");

This Pattern only validates expressions that begin with two 'a' characters and then two or more 'b' characters. But the exercise requires that the two characters a can be anywhere in the sentence and not necessarily at the beginning, as well as the 'b' characters. How do I make this regular expression

    
asked by anonymous 23.02.2017 / 03:01

6 answers

0

Solved. I used the following Pattern:

Pattern pattern = Pattern.compile("(bbb*ab*ab*)|(babb*ab*)|(baabb*)|(ababb*)|(aabbb*)|(abbb*ab*)");
    
23.02.2017 / 14:12
1

What you want is a scan.

The rule says, that it contains:

  • 2 characters a
  • 2 characters b or more

Note that in% w / o the% "or more" is irrelevant because if you have 2 b it is already valid.

Resolution

b

See working at REGEX101 .

Explanation

  • (a.*){2}b.*b|(b.*){2}a.*a|(a.*b|b.*a){2} search for sentences that have (a.*){2}b.*b followed by a , after a followed by b .
  • b search for sentences that have (b.*){2}a.*a followed by b , after b followed by a .
  • a search (a.*b|b.*a){2} followed by a or b followed by b .
24.02.2017 / 15:53
1

Use a positive lookahead .

(?=padrão)

A lookahead allows you to check if the group can be found by starting at the current position but not capturing or advancing the reading of the string being parsed. This way, you can check for two conditions in the same expression.

For example, to check if a string contains at least one "a" character and a "b" character:

^(?=.*a).*b


Regular expression

^[^ab]*+(?=(?:[^b]*b){2})(?:[^a]*a){2}[^a]*$

Online sample


Meaning

  • ^[^ab]*+ - Optional characters that are not a nor b at the beginning of the string.
  • (?=(?:[^b]*b){2}) - Lookahead to check for two b , but does not advance the string reading.
  • (?:[^a]*a){2}[^a]*$ - House exactly two characters a to the end of the string, but not more than two.


Code

import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^[^ab]*+(?=(?:[^b]*b){2})(?:[^a]*a){2}[^a]*$";

String[] exemplos = new String[] { 
    "---aabb+++", "---bbaa+++", "---abab+++", "---baba+++",
    "---babba++", "---bbbbbaa", "ababbb++++", "ccabcab+++",
    "----bcdbaa", "-ababd++++", "bbbaabbbbb", "bbbabbbbbb",
    "bbbaaabbbb", "baaaaaaaaa", "abbbbbbbbb", "ccacbcacbc"
};


final Pattern pattern = Pattern.compile(regex);

for (String palavra : exemplos) {
    Matcher matcher = pattern.matcher(palavra);

    if (matcher.find()) {
        System.out.println(palavra + " ✔️");
    } else {
        System.out.println(palavra + " ✖️️");
    }
}

Result

---aabb+++ ✔️
---bbaa+++ ✔️
---abab+++ ✔️
---baba+++ ✔️
---babba++ ✔️
---bbbbbaa ✔️
ababbb++++ ✔️
ccabcab+++ ✔️
----bcdbaa ✔️
-ababd++++ ✔️
bbbaabbbbb ✔️
bbbabbbbbb ✖️️
bbbaaabbbb ✖️️
baaaaaaaaa ✖️️
abbbbbbbbb ✖️️
ccacbcacbc ✔️

Online sample

    
31.03.2017 / 10:10
0

Try the following:

Pattern pattern = Pattern.compile("(?=^[.[^a]]*?a[.[^a]]*?a[.[^a]]*?$)^.*?b.*?b.*$");
//Use o find ou invés de matches
System.out.println(pattern.matcher("abdbbbabd").find());    //true
System.out.println(pattern.matcher("abdbbbbd").find());     //false
    
23.02.2017 / 15:08
0

Try the default ^((.*[^a])?[a]{2}[b]{2,}.*)$ . Explanation:
- The ^ at the beginning and the $ at the end of the force the complete recognition of the string.
- The (.*[^a]) pattern prevents grouping of more than two a , and ? allows [a]{2}[b]{2,} to be at the beginning of the string.
- The [a]{2}[b]{2,} pattern makes the desired recognition.
- The .* pattern at the end completes the string.

    
24.02.2017 / 14:47
-1

I suggest the following regular expression:

Pattern pattern = Pattern.compile("(aa|bb)");

See the online test .

    
23.02.2017 / 04:05