Does someone know a way on how to check, in Java, if a string containing tags seperated by space, comma or semicolon (or any non-word character) contains a given tag?
For example:
Sample tag string: tag tag_,tag_2;_tag test_3
Check for tag should return true.
Check for test should return false because it the tag string contains test_3 not test.
Check for hello should return false.
Also case shouldn’t matter but there i could just upper the tag string. The tags may contain only character, digit or underscore.
I was trying to use some regex pattern but, even with the help of many post on stackoverlow, i cannot get i to work as i want it.
Thanks.
There’s a couple of possible approaches here. One way is to split the String using a regular expression that matches on whitespace, commas or tabs then compare the split tokens…
The regular expression [\s,;]+ will match one or more spaces (\s – note the double escaping of the regular expression special character \s), semicolons or commas. The String split method will return the array of tokens (in this case tags) separated by values split by tokens matching the regular expression. The tags array should therefore contain all the tag* elements.
Now to check for certain tag elements convert the array to a List and use the List interfaces convenience methods…