Luís Henriques
Luís Henriques

Reputation: 634

Regular Expression for the end of a sentence

I'm trying to match a pattern in Java / Android with regular expressions. The pattern is "consonant" - "vowel" - "s" at the end of a String, like in "similares" and "científicas", but not like in "process".

My code:

Pattern pat = Pattern.compile("[^aeiou][aeiou]s\\b");
Matcher matcher = pat.matcher(word);

Log.e(TAG, word + " contains pattern? " + matcher.matches()  );

This tester says I'm correct:

https://regex101.com/

This one, which is Java specific, tells me I'm correct as well:

https://www.freeformatter.com/java-regex-tester.html#ad-output

But in my Android app, I am unable to make them match.

My output:

activ contains pattern? false
program contains pattern? false
informá contains pattern? false
consult contains pattern? false
process contains pattern? false
dados contains pattern? false
domicili contains pattern? false
inform contains pattern? false
relacion contains pattern? false
outr contains pattern? false
consultoria contains pattern? false
científicas contains pattern? false
técn contains pattern? false
similares contains pattern? false

I also tested for

"[^aeiou][aeiou]s$"

and

"[^aeiou][aeiou][s]\\b"

What am I missing?

Upvotes: 0

Views: 10801

Answers (2)

Randomness Slayer
Randomness Slayer

Reputation: 724

matches tries to match the whole String. If your strings are split up into words and cleaned, you could add .* to match any character any number of times

. - Match any character

* - Quantifier: 0 or more times

^ - Start of String

$ - End of String

^\\b.*[^aeiou][aeiou]s\\b$

However, a caveat to note would be that if your strings aren't cleaned, it also matches non-word characters too.

"all of this whole entire sentence matches", "_./!#pes", "\n\rwas" would all also match.


You could also replace the .* with:

\\w* - Any number of word characters [A-Za-z0-9_]

\\b\\w* - Word boundary followed by any number of word characters

Upvotes: 0

Mehdi
Mehdi

Reputation: 775

This is the regex you would use to match the "consonants" - "vowels" - "s":

^[^AEIOUaeiou]*[AEIOUaeiou]+[s]$

Or, for "everything" - "vowels" - "s":

^[A-Za-z]*[AEIOUaeiou]+[s]$

*: 0 or multiple occurences

+: 1 or multiple occurences

Upvotes: 1

Related Questions