Regex in Java

java

#1

Hi Guru’s,

I need a regular expression in Java for the following cases should satisfy.
Token match is my requirement. Token should at least surrounded by a space. If there is no space before/after/both then it should return false.

My Token is RAGAV, so following condition should satisify

Case -1 : RAGAV --> Matcher should return–> True
Case -2 : ABCRAGAVXYZ --> Matcher should return–> False
Case -3 : ABC RAGAV --> Matcher should return–> True
Case -4 : ABCRAGAV XYZ --> Matcher should return–> True
Case -5: ABC RAGAV XYZ --> Matcher should return–> True

Here i gave an example. Need Regex to satisfy all above condition generic cases. Token can be anything in the Database.

Regards,
Ragav


#2

The best way to learn this is to just start hacking away, and refining your regex.
Java regex tester
Messing around with a tester and developing patterns goes a long way.

Try building a regex that validates telephone numbers as an exercise.