jm li
jm li

Reputation: 313

Use regex to check if a string contains some of the keywords but some are excluded in Java

Let's say given two sets of keywords:

Set #1: apple,banana,pear
Set #2: ?,&,+,version

Want to write a regex to accept any of the strings in Set #1 but exclude any of Set #2.

For example: "This is an apple" is matched but "Is this an apple?" is not.

Upvotes: 0

Views: 25

Answers (1)

aconnelly
aconnelly

Reputation: 627

/^(?=.*(apple|banana|pear))(?!.*(\?|&|\+|version)).*/

Upvotes: 1

Related Questions