News for hackers. Stuff that matters.
Re:Is there a regular expression to detect a valid regular e... by jessaustin on Sunday September 08, 2019 @11:33AM (#20911119)
Just considering the language of balanced parentheses, the only memory needed is a single integer to track depth. If we see a "(", increment. If we see a ")", decrement. If we never go negative and end up with zero, that's a balanced parentheses expression. (To be clear, the parser I've described is not a regex parser.)

Adding the rest of the regex machinery makes this a bit more complex, but in general, if the computer can store the string, it can check whether it's regular.

Re:Is there a regular expression to detect a valid regular e... by taejo on Monday September 09, 2019 @04:08AM (#20916124)
As you say, this is not a regex parser, but some readers may not know the theorem that the regular languages are exactly those which can be parsed by a Turing machine with constant memory, and parenthesis count can have log n bits.

In fact, any Turing machine using o(log log n) space recognizes a regular language (so there is an equivalent machine using O(1) space).

Re:Is there a regular expression to detect a valid regular e... by dyarosla on Sunday September 08, 2019 @12:14PM (#20911401)
This only works if you have a single type of parenthesis. Is that applicable to regex expressions in general that only ()’s can nest?
Re:Is there a regular expression to detect a valid regular e... by gpm on Sunday September 08, 2019 @12:54PM (#20911660)
As typically defined academically, yes. In the real world things might be more complex, but I think it would hold true for many implementations.

Academically a regex is a string of of:

- character literals

- "epsilon" characters (meaning empty string)

- "+" characters (which means either what we see before the + or what we see after the +, in real-world regex these are usually represented as | instead of + as well as used implicitly in constructs like [a-zA-Z])

- "*" characters (same meaning as in real-world regex)

- And parentheses to allow controlling the order of operations

Re:Is there a regular expression to detect a valid regular e... by Piskvorrr on Sunday September 08, 2019 @01:36PM (#20911976)
In other words, "if we build a state machine which is different from a regex, it will perform differently than a regex would." That's trivially true, but is it interesting beyond restating "there is no Silver Bullet"?
Re:Is there a regular expression to detect a valid regular e... by gpm on Sunday September 08, 2019 @01:50PM (#20912069)
Technically we aren't discussing the regex itself here but the syntax for defining a regex.

You could use a different syntax for defining the same regex, and that syntax could have different properties. For instance you could use parantheses like racket where "(...)" and "[...]" have the same meaning, but "(...]" is not well formed syntax (if I remember my racket correctly). Using such a syntax to define regex a counter would no longer suffice to decide if a string is a regex.

Re:Is there a regular expression to detect a valid regular e... by jessaustin on Sunday September 08, 2019 @10:11PM (#20914671)
You could have a stack of (paren-type, integer) pairs. If the paren has a different type than the top pair on the stack (or the stack is empty), push a new pair with the integer set to one. If the integer goes to zero, pop the pair. At the end of the expression, you have to have an empty stack.
Re:Is there a regular expression to detect a valid regular e... by nightfly on Sunday September 08, 2019 @02:01PM (#20912158)
)))((()(()
Re:Is there a regular expression to detect a valid regular e... by gerdesj on Sunday September 08, 2019 @05:59PM (#20913709)
When I check brackets by hand or manually I do actually get my hands out and when I say open I uncurl a finger, and when I say close, I furl a finger. "Open, open, open, close, open, close, close, close". Provided I have no fingers on display at the end, then the brackets are balanced. They might not be in the right place but at least they are balanced. If I find myself starting with close then I close the lid and go and do something else.

I'm sure some sort of rather cheap algorithm falls out of the above. It wont guarantee correctness in what the balanced brackets actually contain but they will at least be balanced.

Re:Is there a regular expression to detect a valid regular e... by sixstringtheory on Sunday September 08, 2019 @02:07PM (#20912210)
Good edge case. I’d say to immediately return an ‘unbalanced’ result once the count goes negative.
Re:Is there a regular expression to detect a valid regular e... by lioeters on Sunday September 08, 2019 @02:54PM (#20912570)
((((()
Re:Is there a regular expression to detect a valid regular e... by detaro on Sunday September 08, 2019 @02:56PM (#20912589)
doesn't end up with zero at the end, rejected?
Re:Is there a regular expression to detect a valid regular e... by lioeters on Sunday September 08, 2019 @06:29PM (#20913817)
I was just trying to suggest a possible edge case, a failing test.
Re:Is there a regular expression to detect a valid regular e... by jessaustin on Sunday September 08, 2019 @10:05PM (#20914653)
"never go negative"