Regular Expression for IP validation which works in JFLAP

722 views Asked by At

I noticed that regular expressions which we programmers use in our programs for tasks such as

  • email address validation
  • IP validation
  • ...

are a bit different from those Regular Expressions which are used in Automata (if I'm not mistaken)

By the way I want to design an NFA and eventually a DFA for IP validation. I have found a lot of regular expression such as the following one:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

But I can not convert it to an NFA or DFA using JFLAP.

What should I do?

1

There are 1 answers

1
Mahmoud Al-Qudsi On

You don't need to directly convert the regex, you can rewrite it once you understand what it's trying to do.

A valid IPv4 address is 4 numbers separated by decimal points. Each number can be from 0 to 255. Regex doesn't do range very well, so that's why it looks like it does. The regex you posted checks if it starts with a 2, then the next two numbers cannot be greater than 5 each, if it starts with 1, they can go up to 9, etc.

Easiest way to validate a regex is to split it with the . as the delimiter, convert the strings to numbers, and check their range.

That said, there is nothing non-standard in the regex you posted. It's as simple as they come, I don't know why it doesn't work as-is for you.