Live data from Hacker News

RegEx101.com now offers a debugger

regex101.com

31–35 of 35 posts

Re: RegEx101.com now offers a debugger

#31
post #28

Earlier quoted context omitted.

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')...

Wrong.

  >>> is_ipv4_addr("01.0.0.0")
  True
It should reject that (i.e. return False) because the first octet contains a leading zero. But you're just stripping the zero away, ignoring its existence. For no effect, because converting with int() already ignores them for you.

Your code is also ok with bizarre inputs like "0..." :-)

Regexes really do have their strengths -- they compactly express a state machine, and you can always break the expression into parts which'll show exactly what the state machine will accept. They could also be much more readable if people bothered to break them into parts instead of typing it out all inside a long string that becomes really difficult to parse visually. There are other notations to improve readability, for example rx in emacs: http://www.emacswiki.org/emacs/rx

A seemingly simple regex can be implemented in imperative code and it might look clean and pretty until you get the logic exactly right and amend it to handle all the corner cases that are not obvious at first sight. For comparison I did the exercise in old-fashioned C (and the indentation got messed up along the way, sigh).

https://pastebin.mozilla.org/3171656

A state machine would be more appropriate in my opinion.

Re: RegEx101.com now offers a debugger

#32

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.

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)

[deleted]

Re: RegEx101.com now offers a debugger

#33
post #31

Earlier quoted context omitted.

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

Wrong. >>> is_ipv4_addr("01.0.0.0") True It should reject that (i.e. return False) because the first octet contains a leading zero. But you're just stripping the zero away, ignoring its existence. For no effect, because converting with int() already ignores them for you. Your code is also ok with bizarre inputs like "0..." :-) Regexes really do have their strengths -- they compactly express a state machine, and you c…

You're right. My mistake.

I like automata and I think regexes are good for some things, but I definitely agree about the crappy syntax. When working in CL, I loved Edi Weitz' CLPPCRE package which allowed you to specify regexes using either the traditional broken string form or an s-exp syntax. Much cleaner.

Re: RegEx101.com now offers a debugger

#34

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…

Ok then maybe it's just a matter of taste. I like the parser approach more:

import Text.ParserCombinators.ReadP -- or the parser lib of your choice

import Data.Char

...

macP = count 16 (satisfy isHexDigit)

Re: RegEx101.com now offers a debugger

#35
post #19

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.

I don't think your example does what you think it does, match fo followed by o zero or more times, followed by bar. when you do understand regex, you'll be amazed at the myriad of things you can do with it.

You are absolutely right, I forgot the '.'.
Post reply on HN