V
V
Vitaly Khvan2021-08-06 08:11:15
Java
Vitaly Khvan, 2021-08-06 08:11:15

Why doesn't the regular expression for Java Pattern work correctly?

There is a regular expression

Pattern pattern = Pattern.compile("^(7)+(701|702|775|778|705|777|771|776|707|747|700|708)+(\\d{7})$");
        Matcher matcher = pattern.matcher(value);


those. according to my logic (probably I'm not thinking correctly),

the first occurrence of 7 is the first digit from the beginning of the string, then you need to find 1 from the list of prepared values, and after 7 any values.

In this case, the value: 77771234567 will work correctly, but if you add one more digit, then the value 7 (2nd character) will fall into group 1 and the second 771

i.e. it turns out that the 1st character is omitted, can someone explain how this can be fixed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sergey, 2021-08-06
@hahenty

The perks need to be removed. Because of them, the regex can consider the correct length of the string to be more than eleven characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question