Live data from Hacker News

RegEx101.com now offers a debugger

regex101.com

21–30 of 35 posts

Re: RegEx101.com now offers a debugger

#22

What is it with the obsession with regular expressions? They are useful things, sure, but I just use them in connection with grep or if I search for strings and normally they are pretty basic, e.g. $ grep -r -n --color "foo*bar" src If I want to validate input data with the machine I just use a parser.

Regexes are an elegant and very powerful way to validate data in scripts in a concise (and if they aren't abused) easy to read fashion. There are almost infinite number of examples, but let's say I want to verify that a field is a 64 Bit hexadecimal MAC address $mac =~ ^[A-Fa-f0-9]{16}$ Gets the job done. How else, but a regular expression so concisely? And, when you say, "If I want to validate input data with the ma…

Here is another one I just did tonight - I wanted to match IPv4 addresses, but didn't want to validate anything with a leading 0 (specifies octal format, which 99.9999% of the time is not what people want), but I do want to accept a leading 0 if it's the only value (I.E. 3.0.2.1, 0.0.0.0, etc...)

regex_ipv4='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$'

Gets the job done.

How else would you do it?

You can then build up a library of these, and use them on other projects.

Re: RegEx101.com now offers a debugger

#23

http://rubular.com/ is also really great! I use it often when programming ruby regular expressions. Fill in a batch of test strings into the box, then run your regular expression against it, and instantly see (visually) what is happening. This is a BIG plus coming from perl regex 10 years ago ;) This is not a dig a perl regex, but I just remember it was a trial and error loop, where I would continually be iterating t…

Looking at regex101, the layout of the page looks awfully similar. It seems that the author took Rubular and made it more generic. So yeah, props to Rubular.

Re: RegEx101.com now offers a debugger

#24

Earlier quoted context omitted.

Regexes are an elegant and very powerful way to validate data in scripts in a concise (and if they aren't abused) easy to read fashion. There are almost infinite number of examples, but let's say I want to verify that a field is a 64 Bit hexadecimal MAC address $mac =~ ^[A-Fa-f0-9]{16}$ Gets the job done. How else, but a regular expression so concisely? And, when you say, "If I want to validate input data with the ma…

Here is another one I just did tonight - I wanted to match IPv4 addresses, but didn't want to validate anything with a leading 0 (specifies octal format, which 99.9999% of the time is not what people want), but I do want to accept a leading 0 if it's the only value (I.E. 3.0.2.1, 0.0.0.0, etc...) regex_ipv4='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$' Get…

> How else would you do it?

Taking your question generally, I was curious to see what it might look like as a parser, since I find that regex a little hard to read. Here's an implementation with Haskell's parsec:

https://gist.github.com/mjhoy/6751909

Re: RegEx101.com now offers a debugger

#27

Earlier quoted context omitted.

Regexes are an elegant and very powerful way to validate data in scripts in a concise (and if they aren't abused) easy to read fashion. There are almost infinite number of examples, but let's say I want to verify that a field is a 64 Bit hexadecimal MAC address $mac =~ ^[A-Fa-f0-9]{16}$ Gets the job done. How else, but a regular expression so concisely? And, when you say, "If I want to validate input data with the ma…

Here is another one I just did tonight - I wanted to match IPv4 addresses, but didn't want to validate anything with a leading 0 (specifies octal format, which 99.9999% of the time is not what people want), but I do want to accept a leading 0 if it's the only value (I.E. 3.0.2.1, 0.0.0.0, etc...) regex_ipv4='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$' Get…

Maybe something like this:

  def is_ipv4_addr(s):
     try:
        octets = s.split('.')
        assert len(octets) == 4
        for o in octets:
            assert 0 
It is longer; on the other hand, it is easier to read and more importantly easier to verify correctness.

Re: RegEx101.com now offers a debugger

#28

Earlier quoted context omitted.

Here is another one I just did tonight - I wanted to match IPv4 addresses, but didn't want to validate anything with a leading 0 (specifies octal format, which 99.9999% of the time is not what people want), but I do want to accept a leading 0 if it's the only value (I.E. 3.0.2.1, 0.0.0.0, etc...) regex_ipv4='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$' Get…

Maybe something like this: def is_ipv4_addr(s): try: octets = s.split('.') assert len(octets) == 4 for o in octets: assert 0 It is longer; on the other hand, it is easier to read and more importantly easier to verify correctness.

I think you forgot to verify that an octet doesn't have leading zeros (unless its value actually is zero).

Re: RegEx101.com now offers a debugger

#29
post #28

Earlier quoted context omitted.

Maybe something like this: def is_ipv4_addr(s): try: octets = s.split('.') assert len(octets) == 4 for o in octets: assert 0 It is longer; on the other hand, it is easier to read and more importantly easier to verify correctness.

I think you forgot to verify that an octet doesn't have leading zeros (unless its value actually is zero).

I didn't forget: the (o.lstrip(0) or '0') expression does that.

Actually, that should be o.lstrip('0')...

Re: RegEx101.com now offers a debugger

#30

Earlier quoted context omitted.

Here is another one I just did tonight - I wanted to match IPv4 addresses, but didn't want to validate anything with a leading 0 (specifies octal format, which 99.9999% of the time is not what people want), but I do want to accept a leading 0 if it's the only value (I.E. 3.0.2.1, 0.0.0.0, etc...) regex_ipv4='^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$' Get…

Maybe something like this: def is_ipv4_addr(s): try: octets = s.split('.') assert len(octets) == 4 for o in octets: assert 0 It is longer; on the other hand, it is easier to read and more importantly easier to verify correctness.

Would:

  1. 12 .13. 14
  089.23.45.67
Both match that? (Your general point is made though - RegExes look fine to the person that just crafted them, but are opaque to the casual observer)
Post reply on HN