tags:

views:

93

answers:

2

Something like this:
http://perl.plover.com/yak/regex/samples/slide083.html

In other words I want to match successfully on { { foo } { bar} } but not on { { foo } .

I see it's possible in perl, and in .NET. Is it possible in emacs regex?

+4  A: 

No, so far Perl/PCRE and .NET are the only regex flavors that support arbitrary nesting (recursive patterns).

Tim Pietzcker
Mmkay, you get an upvote even though this is not the answer I wanted.
Cheeso
Yeah, sorry. But I'm not at all sure if recursive regexes are a Good Thing. They can get complicated enough without that already. A parser combined with a regex looks more sensible to me.
Tim Pietzcker
Meh! "regular expressions" that have been extended to the point that they can do this are not actually "regular" anymore. Not that this is a bad thing, necessarily, but the evolution of the tool has made the terminology obsolete.
dmckee
@dmckee, What makes a regular expression, *regular*? By "not regular" do you just mean that the extensions are not universally supported? I'll buy that. But I did not know that recursion, or the ability to do balanced matching, was an extension, or was not supported. Which is why I asked. @Tim, I think you're right, and that's what I will likely do.
Cheeso
@Cheeso: The term "regular expression" has been stretched over the years. A true regular expression would not allow for backreferences or lookaround since those are not "regular" anymore. Recursiveness is just another extra feature that extends what regular expressions can do - a theoretical computer scientist would probably shudder at the terminology...
Tim Pietzcker
@Cheeso: as Tim said, the "Regular" actually refers to a very specific thing. And the inability to match nesting depths is actually part of the definition of regular. ( http://en.wikipedia.org/wiki/Regular_language#Deciding_whether_a_language_is_regular ). To be fair though, regular expressions engines have been misnamed for at least a decade or two.
quodlibetor
Ah, ok, now I see. Thanks for the link. I don't understand any of that stuff, the Myhill-Nerode theorem or the pumping lemma or free monoids, and I don't care to delve into it. But at least now I am clear that there is a formal definition for "regular".
Cheeso
+2  A: 

No, but if you have a particular use case to discuss you'll often find that you don't need regexes. Simple state-machines to match parenthases are pretty simple to write in lisp. Looking at the source of Paredit is a good place to start.

Singletoned
yes, well in my case the paren parsing is already done. It's within cc-mode, so I can just use `(forward-sexp)` with regex matching on either side. I was just hoping to be able to do it with a single regex.
Cheeso