简体   繁体   中英

REGEX matching problem

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 ?

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

与之匹配:

^(TIM|CNT|ENC)[0-9]+\.[A-Z]+[0-9]$

您可以尝试以下正则表达式: (TIM|CNT|ENC)\\d+\\.\\w+\\d

您应该考虑使用此:

^(TIM|ENC|CNT)\d+\.\w+\d+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM