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
CodePudding user response:
You can make sure that either tin or TIN using the character classes is not present:
^(?![a-zA-Z0-9]*(?:TIN|tin)[a-zA-Z0-9]*$).*(?:TIN|tin).*
(?i)Case insensitive match^Start of string(?![a-zA-Z0-9]*(?:TIN|tin)[a-zA-Z0-9]*$)Assert that TIN or tin (as it is case insensitive, it does not matter for this example) does not occur between alpha numeric chars (no special characters so to say).*(?:TIN|tin).*Match the word in the line
You might add word boundaries \\b(?:TIN|tin)\\b for a more precise match.
Example for a single line:
String s = "CardTIN is 1111";
String[] wordsList = {"TIN","tin"};
String alt = "(?:" String.join("|", wordsList) ")";
String regex = "(?i)^(?![a-zA-Z0-9]*" alt "[a-zA-Z0-9]*$).*" alt ".*";
System.out.println(s.matches(regex));
Output
true
You can also join the list of words on | and then filter the list:
String strings[] = { "CardTIN is 1111", "Card-TIN:2222", "CardTINis3333", "Card@TIN@4444", "CardTIN@5555", "TINis9999", "test", "Card Tin is 1111" };
String[] wordsList = {"TIN","tin"};
String alt = "(?:" String.join("|", wordsList) ")";
String regex = "(?i)^(?![a-zA-Z0-9]*" alt "[a-zA-Z0-9]*$).*" alt ".*";
List<String> result = Arrays.stream(strings)
.filter(word -> word.matches(regex))
.collect(Collectors.toList());
for (String res : result)
System.out.println(res);
Output
CardTIN is 1111
Card-TIN:2222
Card@TIN@4444
CardTIN@5555
Card Tin is 1111
See a Java demo.
CodePudding user response:
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.
