Jennifer
Jennifer

Reputation: 11

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 ?

Upvotes: 1

Views: 69

Answers (4)

aioobe
aioobe

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

Christopher Klewes
Christopher Klewes

Reputation: 11435

You should consider using this:

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

Upvotes: 0

Kobi
Kobi

Reputation: 138007

Match it against:

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

Upvotes: 1

splash
splash

Reputation: 13327

You can try this regex: (TIM|CNT|ENC)\d+\.\w+\d

Upvotes: 0

Related Questions