Regular expression for all positive even numbers

I am currently registered in translation applications, and we are working on regular expressions. This is a homework problem. I was in this for a while, I'm just embarrassed. I do not want to just answer, an explanation would be very useful if I were convinced of this.

I just need a regex for all even numbers, and 0 is fine. Here are some examples of what I need ... (Examples, 0, 00, ... 2, 4, 6, 8, 10, 010, 12, ...)

Thank.

+3
source share
3 answers

For a number to be equal, it must end with an even number. Even the numbers are 0, 2, 4, 6, and 8. Use the character class to indicate which numbers are allowed in each position.

Answer:

/^[0-9]*[02468]$/

Explanation:

^       Start of line/string
[0-9]   Any digit from zero to nine.
*       Repeat the last token zero or more times.
[02468] Any even digit.
$       End of line/string.

, .

\d [0-9]. , \d , .

, , . : : , , . , , .

+8
/\d*[02468]$/         

, \d *. (\ d * ). 0,2,4,6, 8. [02468], $ . , , . , /[abc]/, , "a", "b" "c".

+4

, , , , , 0,2,4,6,8.

0

Source: https://habr.com/ru/post/1764060/


All Articles