user3341564
user3341564

Reputation: 192

Using regex to match consonant letters

I want to search for consonant letters that happens 3 times consecutive i.e "happy" matches, "read" doesn't match

It can be done by:

String str;
if (str.matches("\\S*([b-df-hj-nq-tv-z]){3,}\\S*"))
    System.out.println("yes");
else
    System.out.println("NO");

This code works but is there any way to do this using match a-z except [a,e,i,o,u]?

Upvotes: 2

Views: 7901

Answers (3)

user557597
user557597

Reputation:

This is class subtraction.

Do it the easy way. Internally its done like this.
Externally, you won't make a mistake.

 # "(?i)(?=[^aeiou]{3})[a-z]{3}"

 (?i)
 (?= [^aeiou]{3} )
 [a-z]{3} 

Or you could reverse it for better performance, depending on what is the predominant text you expect.

 # "(?i)(?=[a-z]{3})[^aeiou]{3}"

 (?i)
 (?= [a-z]{3} )
 [^aeiou]{3} 

Upvotes: 0

anubhava
anubhava

Reputation: 785156

In Java you can use this regex i.e. a through z except a, e, i, o, u:

(?i)([a-z&&[^aeiou]]){3,}

RegEx Demo

Java Documentation

Upvotes: 3

Pshemo
Pshemo

Reputation: 124225

match a-z except [a,e,i,o,u]

You can try using [a-z&&[^aeiou]]. && is intersection of sets, so it is like intersection of a-z with characters which are not a,e,i,o,u.

Upvotes: 5

Related Questions