Reputation: 11
My entering string can be in format TIM0.VW0 ( it always starts with TIM or CNT or ENC followed by numbers, then always point and at the end char or chars with digit at end). How to find out if my entering string matches this with regex ?
Upvotes: 1
Views: 69
Reputation: 420951
Something like this could do:
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
String regexp = "(TIM|CNT|ENC)\\d+\\.\\p{Alpha}+\\d";
for (String test : Arrays.asList("TIM0.VW0", "TIM0.VW5", "TIM0.0",
"TIM99.A5", "CNT0VW0", "ABC0.VW0",
"-TIM0.VW0", "TIM9.8x", "ENC0.55"))
System.out.printf("%-10s: %s%n", test, test.matches(regexp));
}
}
Output:
TIM0.VW0 : true
TIM0.VW5 : true
TIM0.0 : false
TIM99.A5 : true
CNT0VW0 : false
ABC0.VW0 : false
-TIM0.VW0 : false
TIM9.8x : false
ENC0.55 : false
Upvotes: 4
Reputation: 11435
You should consider using this:
^(TIM|ENC|CNT)\d+\.\w+\d+$
Upvotes: 0