From: Arthur Chance <{spamtrap}@...> Date: 2003-04-16T22:12:44+09:00 Subject: Re: regular expressions "Chris Pine" writes: > I'm not asking for new regexp syntax in Ruby or anything. I'm sure that > doing it the Perl way is the Right way in this case, and if I used regexps > any more often than I do, I would probably just learn the damn syntax, too, > and that would be the end of it. But, like looking at an old Perl program, > an old regexp is just... To a great extent the Perl way with regexps is the Unix way - a terse notation for serious, regular users. It's not ideal for intermittent users. > Then I started thinking: We have Perl-style regexps, right? What other > style regexps are there? I'm sure people have come up with a number of > different ideas, but a quick Google didn't turn up anything. Does anyone > know of any other ideas? Well, a long time ago, in a very different world, there was a pattern matching language called Snobol (or rather SNOBOL as punched cards didn't have lower case :-). 30+ years ago I used to use it for the sort of quick string manipulating hacks that Ruby and Perl often get used for today. SNOBOL had "patterns" which weren't exactly today's regexps but were very similar. I've often thought that a modern variation of Snobol patterns would make a more user friendly regexp syntax. [5 minutes later and I've found my old SNOBOL4 language manual, copyright 1971. This takes me back.] Here are SNOBOL4's basic patterns, in the order listed in the manual. Patterns could be enclosed in parentheses, alternatives could be separated by "|", and juxtaposition of patterns meant concatenation like in regexps. Using a variable in a pattern meant taking its contents as a pattern. The string matched by a pattern could be assigned to a variable by appending either $ variable or . variable. In the former case the match was assigned as the matching was working so could be used with "*" to act like \1, \2, etc in regexps. A $ var match could be reassigned if backtracking caused another match to happen. In the . var case the assignment only happened after the entire match worked. SNOBOL Perl/Ruby pattern regexp LEN(3) .{3,3} SPAN('aeiou') [aeiou]+ BREAK('aeiou') [^aeiou]+ ANY('aeiou') [aeiou] NOTANY('aeiou') [^aeiou] TAB(n) No exact regexp equivalent. Match all (maybe 0) chars from current position to just before the nth char. If beyond the nth character, fail. RTAB(n) Like TAB, but from end of string rather than beginning. POS(n) Match if and only if at nth character. POS(0) is equivalent to regexp ^. RPOS(n) Like POS, but from the right. RPOS(0) = $ FAIL Always fails to match, forcing alternatives to be tried. FENCE Succeeds when first matched, if backtracking causes the character position to try to back up through the fence, the match fails. ABORT Aborts the entire match, including untried alternatives. *variable Match the pattern (usually a simple string) in the variable at the time the * pattern is tried for a match. With a prior $ variable assignment this acts the same as \1 etc in regexps. ARB .*? (i.e. minimum match of .*) ARBNO(pattern) (pattern)* BAL Matched any string containing balanced, possibly nested, parentheses. Impossible for regexp I believe. SUCCEED Succeeds when first matched, if backtracking causes the character position to try to back up through the SUCCEED then it matches again and starts the match going forward. Sort of a reflective FENCE. You don't want to know what eveil tricks this could be used for. Hope that wasn't too much like trying to drink from a firehose. :-) It might give someone an idea. Just don't ask about Snobol's control flow mechanisms, you really don't want to know. -- Conspiracy theories exist to prevent you knowing what's really going on.