1

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

2
  • if (element.matches("(?i)CardTIN is \\d{4}|Card-TIN:\\d{4}|Card\\@TIN\\@\\d{4}|CardTIN\\@\\d{4}")) {. Commented Jan 27, 2022 at 14:37
  • there are many numerous test cases given are the 5 sample scenarios Commented Jan 27, 2022 at 14:57

2 Answers 2

1

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.

Regex demo

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.

Sign up to request clarification or add additional context in comments.

8 Comments

The test case[ Card Tin is 1111] should also match which is failing in this expression. Also the tin and Tin are in an array list and it has many tag values it is not limited to two tag values
@dhanju Then you can make the pattern case insensitive like this regex101.com/r/Y0KMPe/1 and for the array with tag values, you can create an alternation like this String alternation = "(?:" + String.join("|", wordsList) + ")";
@dhanju Something like this ideone.com/k5opvF
The Scenarios are not in a list , it is consumed one by one eg : CARDTIN is 1111 is a string it is not a part of list values
@dhanju You join the values of the wordsList with | to a grouping structure that will be part of the regex. Then you loop / filter the list of strings that you have, where one of those strings can be CARDTIN is 1111 Did you check the Java demo link ?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.