String temp = "77"; // It can be 0 or 100 or any value
// So the pattern will be like this only but number can be change anytime
String inclusion = "100;0;77;200;....;90";
I need to write a regular expression so that I can see whether temp exists in inclusion or not so for that I wrote a regexPattern like this.
// This is the regular Expression I wrote.
String regexPattern = "(^|.*;)" + temp + "(;.*|$)";
So do you think this regular expression will work everytime or there is some problem with that regexPattern?
if(inclusion.matches(regexPattern)) {
}
You could run into issues if
tempcan contain special characters for regular expressions, but if it is always integers then your method should be fine.However, a more straightforward way to do this would be to split your string on semi-colons and then see if
tempis in the resulting array.If you do stick with regex, you can simplify it a bit by dropping the
.*, the following will work the same way as your current regex:edit: Oops, the above will actually not work, I am a bit unfamiliar with regex in Java and didn’t realize that the entire string needs to match, thanks Affe!