Combine two regular expressions

I want to return trueif the line starts with a consonant. I have two conditions and I don’t know how to combine them.

1) this letter 2) it is not a vowel

!!(s[0] =~ /([a-z]&&[^aeiou])/i)

I tried all sorts of other syntaxes like:

!!(s[0] =~ /([a-z])([^aeiou])/i)
!!(s[0] =~ /(([a-z])([^aeiou]))/i)

Is there a way to do this in one regex? Do I need to check each condition separately?

+4
source share
3 answers

You can combine character classes with the operator &&:

/[a-z&&[^aeiou]]/

Note that the operator is &&used inside the character class, not later.

In the documentation:

. isnt , [a-z[0-9]] , [a-z0-9]. &&, . :

/[a-w&&[^c-g]z]/ # ([a-w] AND ([^c-g] OR z))
# This is equivalent to:
/[abh-w]/
+8

, , .

\A(?=[^aeiou])(?=[a-z])

.

(?i:(?![aeiou])[a-z]\w*)
+3
/^[bcdfghjklmnpqrstvwxyz]/i

, fancier.

+1

All Articles