Skip to content
Advertisement

Java Regex Handling

Looking for a java regex function to 1 – return true if Special characters present before or after from my list of array elements

2 – Return False if any alpha characters present before and after from my list of array elements My array elements wordsList = {“TIN”,”tin”} Input:

1-CardTIN is 1111 2-Card-TIN:2222 3-CardTINis3333 4-Card@TIN@4444 5-CardTIN@5555 6-TINis9999

Expected Output:

1-True 2-True 3-False 4-True 5-True 6-False

I have tried regex function to cover these cases

Arrays.stream(wordsList).anyMatch(word -> Pattern .compile(“([^a-zA-Z0-9])” + Pattern.quote(word) +”([^a-zA-Z0-9])”.matcher(string).find()

But the scenario CardTin@555 is not giving the desired result as expected

Kindly help with these case

Advertisement

Answer

I am not sure whether your requirements can be put in regex. If at all, you are running way into specialities that will become hard to maintain.

Therefore you might be better off using a lexer/scanner combination which usually make up a complete parser. Check out ANTLR.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement