Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression, calculate something like CRC in it with validations

Tags:

java

regex

I have an array of bytes, I need to check if it starts with a specific number 0, terminates with 9 and in-between could be anything numbers, but after 9, I get the sum of the numbers.

e.g test[] = { 0, 1, 4, 5, 9, 10 };
test[] = { 0, 3, 2, 9, 6, 0, 4, 2, 9, 6 }; - in this there are two sets

Is there way using Regular Expression to find this?

like image 539
Vivek Avatar asked Sep 10 '25 19:09

Vivek


1 Answers

This doesn't look like a regular language to me, so the answer is no, you can't do it.

If the size of the list was fixed or you needed the sum of the numbers modulo 10 for example, it would be very complicated, but probably doable. In general though, a language in which the words consist of a list of numbers and the sum of them isn't regular, as it trivially fails to satisfy the pumping lemma.

On the positive side, it can easily be done without regular expressions.

like image 138
biziclop Avatar answered Sep 13 '25 11:09

biziclop