pkc
pkc

Reputation: 8516

Regular expression for specific character start and end

I am writing regular expression for words which start with T and ends with A.

I write:-

^T\..*A$

But now I want to exclude the word TEA. How to exclude the specific word using the regular expression.

Upvotes: 4

Views: 3275

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626936

Your ^T\..*A$ regex matches a string that starts with T., then has any 0+ chars other than line break chars (with .*) and then ends with A.

To match any string that starts with T and ends with A that is not equal to TEA you may use

(?s)^(?!TEA$)T.*A$

To make it case insensitive, add i modifier:

(?si)^(?!TEA$)T.*A$

See the regex demo.

Details

  • (?si) - DOTALL (s) modifier to allow . to match any char and IGNORECASE (i) modifier to make the pattern case insensitive
  • ^ - start of string
  • (?!TEA$) - a negative lookahead that fails the match if the whole string is equal to TEA
  • T - a T
  • .* - 0+ chars, as many as possible
  • A - A letter
  • $ - end of string.

Note that if you are using it in matches() method, the first ^ and last $ can be removed.

Upvotes: 2

kimkevin
kimkevin

Reputation: 2222

If you want to find 'TEA' and 'TEEA' from this sample, 'TEA IS NOT TEEA'. You can try to use this regular expression.

Pattern p = Pattern.compile("((T|t)[A-Za-z]+(A|a))");
Matcher m = p.matcher("TEA or tea IS NOT TEEA or teea");
while(m.find()) {
    System.out.println(m.group(1));
}

Upvotes: 1

Related Questions