# Alternate Regular Expressions?

**URL:** <https://rubytalk.org/t/alternate-regular-expressions/39716>\
**Category:** ruby-talk\
**Created:** [7 August 2007 01:12 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716 "2007-08-07T01:12:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [7 August 2007 01:12 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/1 "2007-08-07T01:12:20Z")

</div>

Just randomly curious -

Is there an alternate RegExp "language" to the current one in Ruby and Perl?

Don't get me wrong, I love the current RegExp in Ruby, but I'm allowed to be curious...

Also, is Ruby going to jump on the PERL 6 RegExp ship?

^^^^^^^ That's a big one to some people I know.

Thanks,  
~ Ari  
English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [7 August 2007 01:40 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/2 "2007-08-07T01:40:30Z")

</div>

Ari Brown wrote:

> Just randomly curious -
> 
> Is there an alternate RegExp "language" to the current one in Ruby and Perl?

I don't know. So here's a dissertation on where to start.

The good news is a RegExp is only two things at heart...

- a Domain-Specific Language to program  
- a state machine.

The bad news is, back in the day, people used to invent DSL as long strings of easily parsed characters. For example, a language called LSYSTEM might describe turtle graphics like this:

s=[::cc!!!!&&[FFcccZ]^^^^FFcccZ] # upper spikes

The really bad news is RegExp is one of these string-oriented DSLs that stuck. It will always be useful, so programmers forget how much room it has for improvement.

The good news is Ruby excels at generating light DSLs. The equivalent expression for a modern implementation of LSYSTEM might look like this:

upper\_spikes = push.twist(2).thinner(2).increase\_angle(4)....

etc. Because Ruby gives your programming interfaces extreme notational flexibility, you can declare the interfaces most convenient for your domain.

So start writing! and research other DSLs as you go. For example, here's a DSL written with C++ metaprogramming:

[http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/index.html](http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/index.html)

Whenever you like, that language slips back to raw RegExp. Your effort should have a similar shunt.

> English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.

Of all the world's languages, English is both the ugliest and the beautifulest.

> **···**
>
> --  
> &nbsp;&nbsp;Phlip  
> &nbsp;&nbsp;[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
> &nbsp;&nbsp;"Test Driven Ajax (on Rails)"  
> &nbsp;&nbsp;assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Donn](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Wolfgang\_Nadasi-Donn](https://rubytalk.org/u/Wolfgang_Nadasi-Donn)\
**Post date:** [7 August 2007 11:01 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/3 "2007-08-07T11:01:17Z")

</div>

Ari Brown wrote:

> Is there an alternate RegExp "language" to the current one in Ruby  
> and Perl?

Snobol4 pattern are now available as a Python library. It should be  
possible to port it to Ruby. I don't think that the implementation is  
complete, because I didn't see the possibility of recursive pattern  
definitions, which give Snobol4 the extreme power.

Infos

[http://permalink.gmane.org/gmane.comp.python.announce/7217](http://permalink.gmane.org/gmane.comp.python.announce/7217) (Snobol4 in  
Python)

[SNOBOL - Wikipedia](http://en.wikipedia.org/wiki/SNOBOL) (has some links)

Wolfgang Nádasi-Donner

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [24 December 2009 02:27 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/4 "2009-12-24T02:27:46Z")

</div>

Ari Brown wrote:

> Just randomly curious -
> 
> Is there an alternate RegExp "language" to the current one in Ruby  
> and Perl?

There's a RegExp library for Common Lisp that, besides a string, accepts  
also a "parse tree". See [CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp](http://weitz.de/cl-ppcre/)

The regexp string...

&nbsp;&nbsp;&nbsp;&nbsp;(?:abc){3,5}

...is equal to the following data structure:

&nbsp;&nbsp;&nbsp;(:GREEDY-REPETITION 3 5 (:GROUP "abc"))

which, in Ruby, looks like:

&nbsp;&nbsp;&nbsp;[:GREEDY-REPETITION, 3, 5, [:GROUP "abc"]]

This is not really a different language, just a way to express the  
regexp string as a data-structure.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [7 August 2007 01:58 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/5 "2007-08-07T01:58:03Z")

</div>

Ugh. If I must (which I must). What would you suggest as syntax?

Also, should I completely try to reinvent the wheel, or create a wrapper for current RegExp?

Man. I need a mentor on this 😑

aRi  
--------------------------------------------|  
IMO, Arabic has THE most beautiful script.  
Poetically, English is extremely beautiful. It's like a language of RegExp - except there are no rules!  
Spoken, the most beautiful language is either French (sorry) or Esperanto.

> **···**
>
> On Aug 6, 2007, at 9:40 PM, Phlip wrote:
> 
> > So start writing! and research other DSLs as you go.

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [7 August 2007 02:08 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/6 "2007-08-07T02:08:50Z")

</div>

Ari Brown wrote:

> > So start writing! and research other DSLs as you go.
> 
> Ugh. If I must (which I must). What would you suggest as syntax?
> 
> Also, should I completely try to reinvent the wheel, or create a wrapper for current RegExp?
> 
> Man. I need a mentor on this 😑

This might give you a place to start: [Parsing expression grammar - Wikipedia](http://en.wikipedia.org/wiki/Parsing_expression_grammar)

> **···**
>
> > On Aug 6, 2007, at 9:40 PM, Phlip wrote:
> 
> --  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/\](http://rubyforge.org/projects/rmagick/%5C)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618\](http://rubyforge.org/forum/forum.php?forum_id=1618%5C)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html\](http://rmagick.rubyforge.org/install-faq.html%5C)]

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://rubytalk.org/u/Kenneth_McDonald)\
**Post date:** [7 August 2007 02:51 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/7 "2007-08-07T02:51:55Z")

</div>

Ari,

How serious are you about this? Several years ago I wrote a Python library that treats Python regular  
expressions as semantic, not syntactic, objects, and that has been incredibly useful to me. I've started  
to port it to Ruby, but simply don't have the time. If you do (you're probably looking at a couple of  
weeks of full-time-equivalent hours to do a good job, including decent documentation), I'm happy to pass  
on the Python code, the Ruby code, and give advice and so on.

To help you evaluate this, and also as a potential source of ideas in case you do something else, I've  
appended my (probably out of date) intro text to the library at the bottom of this reply.

Cheers,  
Ken

Ari Brown wrote:

> > So start writing! and research other DSLs as you go.
> 
> Ugh. If I must (which I must). What would you suggest as syntax?
> 
> Also, should I completely try to reinvent the wheel, or create a wrapper for current RegExp?
> 
> Man. I need a mentor on this 😑
> 
> aRi  
> --------------------------------------------|  
> IMO, Arabic has THE most beautiful script.  
> Poetically, English is extremely beautiful. It's like a language of RegExp - except there are no rules!  
> Spoken, the most beautiful language is either French (sorry) or Esperanto.

Text from the \_Python\_ library (In retrospect, I would do quite a bit different):

&nbsp;&nbsp;&nbsp;&nbsp;Overview

> **···**
>
> > On Aug 6, 2007, at 9:40 PM, Phlip wrote:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;========  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rex' provides regular expression and parsing facilities. It uses (and is intended to functionally  
> &nbsp;&nbsp;&nbsp;&nbsp;replace) the Python 're' module.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Regular expression functionality is provided through the '\_Rexp' and 'MatchResult' classes,  
> &nbsp;&nbsp;&nbsp;&nbsp;and the CHAR, REP0, REP1, OPT, PAT, and ALT constructs.  
> &nbsp;&nbsp;&nbsp;&nbsp;These constructs can be used as or provide functions to create rexps, and also define  
> &nbsp;&nbsp;&nbsp;&nbsp;attributes for commonly used rexps. (For example, PAT.float provides a rexp  
> &nbsp;&nbsp;&nbsp;&nbsp;which matches basic floating-point (no exponent) numbers.)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pattern-Matching Example  
> &nbsp;&nbsp;&nbsp;&nbsp;----------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you are familiar with regular expressions, the following will probably make at  
> &nbsp;&nbsp;&nbsp;&nbsp;least some sense. If you are not, skip this example for now. In either case, come  
> &nbsp;&nbsp;&nbsp;&nbsp;back to it once you have have read the formal definitions of functions and  
> &nbsp;&nbsp;&nbsp;&nbsp;constructs provided by rex.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMPLEX= PAT.float['re'] + \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;REP0.whitespace + \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALT("+", "-")['op'] + \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;REP0.whitespace + \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAT.float['im'] + \  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'i'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;The above example defines a pattern which will match complex  
> &nbsp;&nbsp;&nbsp;&nbsp;numbers, of the form "-2.718 + 3.14i", for example. It uses the predefined  
> &nbsp;&nbsp;&nbsp;&nbsp;match expressions PAT.float and REP0.whitespace to  
> &nbsp;&nbsp;&nbsp;&nbsp;ease the definition. Applied to the example complex number string, the result will contain three  
> &nbsp;&nbsp;&nbsp;&nbsp;named substrings: 're' will map to "-2.718", "op" will map to "+", and "im" will map to "3.14".  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SEQ is an alternative form of joining rexps; the above is equivalent to:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMPLEX= SEQ(  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAT.float['re'], REP0.whitespace,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALT("+", "-")['op'],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;REP0.whitespace,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAT.float['im'],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'i'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Regular Expressions  
> &nbsp;&nbsp;&nbsp;&nbsp;---------------
> 
> &nbsp;&nbsp;&nbsp;&nbsp;This is an introduction to using the pattern-matching (regular-expression-related)  
> &nbsp;&nbsp;&nbsp;&nbsp;part of rex. See documentation associated  
> &nbsp;&nbsp;&nbsp;&nbsp;with a specific method/function/name for details on that entity.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the following, we use the abbreviation RE to refer to standard regular  
> &nbsp;&nbsp;&nbsp;&nbsp;expressions defined as strings, and the word 'rexp' to refer to rex objects  
> &nbsp;&nbsp;&nbsp;&nbsp;which denote regular expressions.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The starting point for building a rexp is either rex.PAT,  
> &nbsp;&nbsp;&nbsp;&nbsp;which we'll just call PAT, or rex.CHAR, which we'll just call CHAR, or rex.LIT.  
> &nbsp;&nbsp;&nbsp;&nbsp;CHAR provides rexps defining a set of characters, and which  
> &nbsp;&nbsp;&nbsp;&nbsp;will match a single character string if that character is in the given  
> &nbsp;&nbsp;&nbsp;&nbsp;set. In addition to providing attributes which provide prebuilt character  
> &nbsp;&nbsp;&nbsp;&nbsp;sets, the CHAR function may be used to define your own character  
> &nbsp;&nbsp;&nbsp;&nbsp;sets.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIT builds rexps which match strings of varying lengths.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;REP0 and REP1 are zero or more and 1 or ore  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Also  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- PAT.\_someattribute\_ returns (for defined attributes) a corresponding rexp.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For example, PAT.stringstart returns a rexp matching at the start of a string.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- CHAR(a1, a2, . . .) returns a rexp matching a single character from a set  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of characters defined by its arguments. For example, CHAR("-", ["0","9"], ".")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iter the characters necessary to build basic floating point numbers.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See CHAR docs for details.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- CHAR.\_someattribute\_ returns (for defined attributes) a corresponding rexp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defining a set of characters.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For example, CHAR.digit returns a rexp matching a single digit.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Now assume that A, B, C,... are rexps. The following Python expressions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\_not\_ strings) may be used to build more complex rexps:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X | Y | Z . . . : returns a rexp which iter a string if any of the operands  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;match that string. Similar to "X|Y|Z" in normal REs, except of course you can't  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use Python code to define a normal RE.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X + Y + Z ...: returns a rexp which iter a string if all of X, Y, Z match consecutive  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;substrings of the string in succession. Like "XYZ" in normal REs.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X\*n : returns a rexp which iter a number of times as defined by n.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This replaces '?', '+', and '\*' as used in normal REs. See docs for details.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rex' defines constants which allow you to say X\*REP0, X\*REP1, or X\*MAYBE,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indicating (0 or more iter), (1 or more iter), or (0 or 1 iter),  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;respectively.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X\*\*n : Like X\*n, but does nongreedy matching.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- +X : positive lookahead assertion: iter if X iter, but doesn't  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;consume any of the input.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ~+X : negative lookahead assertion: iter if X \_doesn't\_ match,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;but doesn't consume any of the input.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- -X, ~-X : positive and negative lookback assertions. Lke lookahead assertions,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;but in the other direction.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X[name] : name must be a string: any matched by X can be referred  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to by the given name in the match result object. (This is the equivalent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;of named groups in the re module).  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- X.group() : X will be in an unnamed group, referable by number.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In addition, a few other operations may be performed:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Some of the attributes defined in PAT have "natural inverses"; for such  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attributes, the inverse may be taken. For example, ~PAT.digit is  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a pattern matching any character except a digit.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Character classes may be inverted: ~CHAR("aeiouAEIOU") returns a pattern  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matching any except a vowel.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'ALT' gives a different way to denote alternation: ALT(X, Y, Z,...) does  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the same thing as X | Y | Z | . . ., except that none of the arguments  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to ALT need be rexps; any which are normal strings will be converted  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to a rexp using PAT.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'SEQ' can take multiple arguments: PAT(X, Y, Z,...), which gives the same  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result as PAT(X) + PAT(Y) + PAT(Z) + . . . .  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Finally, a very convenient shortcut is that only the first object in a sequence of  
> &nbsp;&nbsp;&nbsp;&nbsp;operator/method calls needs to be a rexp; all others will be automatically  
> &nbsp;&nbsp;&nbsp;&nbsp;converted as if LIT(...) had been called on them. For example, the  
> &nbsp;&nbsp;&nbsp;&nbsp;sequence X | "hello" is the same as X | LIT("hello")

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [7 August 2007 02:51 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/8 "2007-08-07T02:51:56Z")

</div>

Ari Brown wrote:

> Ugh. If I must (which I must).

You missed where I said I didn't know the actual answer.

> What would you suggest as syntax?

Ruby itself, as a DSL; that was the point.

&nbsp;&nbsp;&nbsp;&nbsp;rx = match('foo') or match('bar') # like /(foo|bar)/  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal [['foo', 'bar']], rx('a foo b bar')

Make match() return an object that overloads the or operator, and away you go!

> **···**
>
> --  
> &nbsp;&nbsp;Phlip  
> &nbsp;&nbsp;[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
> &nbsp;&nbsp;"Test Driven Ajax (on Rails)"  
> &nbsp;&nbsp;assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [20 December 2009 05:00 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/9 "2009-12-20T05:00:00Z")

</div>

Ari Brown wrote:

> > So start writing! and research other DSLs as you go.
> 
> Ugh. If I must (which I must). What would you suggest as syntax?
> 
> Also, should I completely try to reinvent the wheel, or create a  
> wrapper for current RegExp?
> 
> Man. I need a mentor on this 😑

I would suggest taking a look at Treetop, both as an easy-to-use parser  
generator and as an inspiration for regexp extensions. But I mostly  
like regexps the way they are.

> aRi  
> --------------------------------------------|  
> IMO, Arabic has THE most beautiful script.

Ever looked at Mongolian (Uighur) script?

> Poetically, English is extremely beautiful. It's like a language of  
> RegExp - except there are no rules!

Uh, what? (I know that was intended to be funny -- I just don't get  
it.)

> Spoken, the most beautiful language is either French (sorry) or  
> Esperanto.

Hmmm...

Best,

> **···**
>
> > On Aug 6, 2007, at 9:40 PM, Phlip wrote:
> 
> --  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [7 August 2007 03:10 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/10 "2007-08-07T03:10:12Z")

</div>

I'm moderately serious. This is going to be one of those projects that won't see the light of day for maybe 6 months to a year.  
This looks largely what I was hoping to make, although in Ruby I had invisioned this:

matching email addresses (sample):  
a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
)

case line  
when a  
&nbsp;&nbsp;# ...

My idea is to make it logical and human readable. Ruby is a language for humans and UberBeings, and I think this should reflect Ruby's ideas.

Also, was you library a wrapper for underlying PERL RegExp? or was it the whole RegExp engine?

Thanks,  
Ari

> **···**
>
> On Aug 6, 2007, at 10:51 PM, Kenneth McDonald wrote:
> 
> > Ari,
> > 
> > How serious are you about this? Several years ago I wrote a Python library that treats Python regular  
> > expressions as semantic, not syntactic, objects, and that has been incredibly useful to me. I've started  
> > to port it to Ruby, but simply don't have the time. If you do (you're probably looking at a couple of  
> > weeks of full-time-equivalent hours to do a good job, including decent documentation), I'm happy to pass  
> > on the Python code, the Ruby code, and give advice and so on.
> > 
> > To help you evaluate this, and also as a potential source of ideas in case you do something else, I've  
> > appended my (probably out of date) intro text to the library at the bottom of this reply.
> > 
> > Cheers,  
> > Ken
> 
> --------------------------------------------|  
> If you're not living on the edge,  
> then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [21 December 2009 15:39 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/11 "2009-12-21T15:39:15Z")

</div>

Phlip wrote:

> &nbsp;&nbsp;&nbsp;&nbsp;rx = match('foo') or match('bar') # like /(foo|bar)/

Aside: you either need to use '||' instead of 'or', or you need extra  
parentheses, i.e.

&nbsp;&nbsp;&nbsp;rx = (match('foo') or match('bar'))

otherwise it parses as

&nbsp;&nbsp;(rx = match('foo')) or match('bar')

I don't really have a problem with regexps as they are. Although I'd  
like to have more limited, \*true\* regexps, which compile to a DFA and  
never backtrack.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://rubytalk.org/u/Kenneth_McDonald)\
**Post date:** [7 August 2007 03:29 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/12 "2007-08-07T03:29:58Z")

</div>

Ari Brown wrote:

> I'm moderately serious. This is going to be one of those projects that won't see the light of day for maybe 6 months to a year.  
> This looks largely what I was hoping to make, although in Ruby I had invisioned this:
> 
> matching email addresses (sample):  
> a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
> )
> 
> case line  
> when a  
> &nbsp;&nbsp;&nbsp;&nbsp;# ...
> 
> My idea is to make it logical and human readable. Ruby is a language for humans and UberBeings, and I think this should reflect Ruby's ideas.

Reflecting on my own experience, I'd suggest a less verbose notation, and one that uses Ruby idioms more. For example:

letters = CharClass.new('a'..'z').case\_insensitive  
a = letters + "@" + letters + "." + (Literal.new("com") | "net" | "org"

> "edu")

It's not at all difficult to do this with Ruby. Strings can be used for literals and character classes, and  
ranges are perfect for use as char ranges in character classes.

Also, the ability to safely combine regular expressions (as shown above, where "letters" is used in "a")  
is \_paramount\_ in making this sort of wrapper really useful.

> Also, was you library a wrapper for underlying PERL RegExp? or was it the whole RegExp engine?

It was in Python; instances of my 'rex' class simply construct and use Python patterns, and their associated  
functions, internally and invisibly to the user.

Ken

> **···**
>
> > Thanks,  
> > Ari

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [7 August 2007 05:34 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/13 "2007-08-07T05:34:57Z")

</div>

> I'm moderately serious. This is going to be one of those projects that won't see the light of day for maybe 6 months to a year.  
> This looks largely what I was hoping to make, although in Ruby I had invisioned this:
> 
> matching email addresses (sample):  
> a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
> )

You cannot do this because Hashes are unordered so you loose the original order. Also [a-z] is only valid if you define local variables a and z.

Personally I find regular expressions pretty readable - at least if they are crafted properly. 🙂 See also below.

> case line  
> when a  
> &nbsp;&nbsp;&nbsp;&nbsp;# ...
> 
> My idea is to make it logical and human readable. Ruby is a language for humans and UberBeings, and I think this should reflect Ruby's ideas.

Do you know the /x modifier? Than can go a long way to make a regular expression readable. For example:

input = \<\<TEXT  
adjasdkajda dadkajd foo@bar.com adklskkdaldjskj  
postmaster@root.edu adkjasdjk  
blah@org akjsd askdl asd noname@foo.net hello  
asdj  
TEXT

input.scan %r{  
&nbsp;&nbsp;&nbsp;\b # word boundary  
&nbsp;&nbsp;&nbsp;(?i:[a-z]+) # user name  
&nbsp;&nbsp;&nbsp;@ # the famous "at" sign  
&nbsp;&nbsp;&nbsp;(?i:[a-z]+) # host name  
&nbsp;&nbsp;&nbsp;\. # a literal dot  
&nbsp;&nbsp;&nbsp;(?:com|net|org|edu) # only some of the TLDs  
&nbsp;&nbsp;&nbsp;\b # word boundary  
}x do |match|  
&nbsp;&nbsp;&nbsp;puts "Found email address #{match}"  
end

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 07.08.2007 05:10, Ari Brown wrote:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 August 2007 08:52 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/14 "2007-08-07T08:52:56Z")

</div>

Hi --

> I'm moderately serious. This is going to be one of those projects that won't see the light of day for maybe 6 months to a year.  
> This looks largely what I was hoping to make, although in Ruby I had invisioned this:
> 
> matching email addresses (sample):  
> a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
> )
> 
> case line  
> when a  
> &nbsp;&nbsp;# ...
> 
> My idea is to make it logical and human readable. Ruby is a language for humans and UberBeings, and I think this should reflect Ruby's ideas.

Regular expressions are nothing if not logical 🙂 And readability,  
as always, is largely in the eye of the beholder. I think the quest  
for an alternative notation is fine, but there's nothing inherently  
un-Ruby-like about what's there already. Then again, I'm in a small  
minority who find /x with a lot of extra whitespace a serious  
impediment to understanding a pattern 🙂

Anyway -- somewhere out there, though I haven't been able to find it,  
is a library called Regexp::English by Florian Gross, which provides a  
kind of English-language wrapper for regexes. I don't know whether  
it's still in development and/or at a point of usability.

David

> **···**
>
> On Tue, 7 Aug 2007, Ari Brown wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Yossef\_Mendelssohn](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yossef\_Mendelssohn](https://rubytalk.org/u/Yossef_Mendelssohn)\
**Post date:** [7 August 2007 13:11 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/15 "2007-08-07T13:11:27Z")

</div>

Ari,

There have been other responses to this already, but I thought I'd  
give you something else to look at:

> **[That's not fluent...](https://bofh.org.uk/2007/03/15/thats-not-fluent/)**
>
> So, I’m not a fan of static typing. It’s okay in the likes of Haskell which does type inferencing and generally goes out of its way to reduce programmer pain, but Java? C#? No ta. It’s awfully tempting to conclude that anyone who chooses to use those...

I second (or third, or whatever) the contention that regular  
expressions are pretty readable on their own (given some knowledge of  
the syntax and good formatting). The thing to keep in mind is that  
they're a language of their own. Once you learn the language, you  
find you can use it in many a programming language (though there are  
some dialectical problems here and there).

> **···**
>
> On Aug 6, 10:10 pm, Ari Brown \<a...@aribrown.com\> wrote:
> 
> > I'm moderately serious. This is going to be one of those projects  
> > that won't see the light of day for maybe 6 months to a year.  
> > This looks largely what I was hoping to make, although in Ruby I had  
> > invisioned this:
> > 
> > matching email addresses (sample):  
> > a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
> > )
> > 
> > case line  
> > when a  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...
> > 
> > My idea is to make it logical and human readable. Ruby is a language  
> > for humans and UberBeings, and I think this should reflect Ruby's ideas.
> > 
> > Also, was you library a wrapper for underlying PERL RegExp? or was it  
> > the whole RegExp engine?
> > 
> > Thanks,  
> > Ari
> > 
> > On Aug 6, 2007, at 10:51 PM, Kenneth McDonald wrote:
> > 
> > \> Ari,
> > 
> > \> How serious are you about this? Several years ago I wrote a Python  
> > \> library that treats Python regular  
> > \> expressions as semantic, not syntactic, objects, and that has been  
> > \> incredibly useful to me. I've started  
> > \> to port it to Ruby, but simply don't have the time. If you do  
> > \> (you're probably looking at a couple of  
> > \> weeks of full-time-equivalent hours to do a good job, including  
> > \> decent documentation), I'm happy to pass  
> > \> on the Python code, the Ruby code, and give advice and so on.
> > 
> > \> To help you evaluate this, and also as a potential source of ideas  
> > \> in case you do something else, I've  
> > \> appended my (probably out of date) intro text to the library at the  
> > \> bottom of this reply.
> > 
> > \> Cheers,  
> > \> Ken
> > 
> > --------------------------------------------|  
> > If you're not living on the edge,  
> > then you're just wasting space.
> 
> --  
> -yossef

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [8 August 2007 00:24 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/16 "2007-08-08T00:24:57Z")

</div>

Ari Brown wrote:

> I'm moderately serious. This is going to be one of those projects that won't see the light of day for maybe 6 months to a year.  
> This looks largely what I was hoping to make, although in Ruby I had invisioned this:
> 
> matching email addresses (sample):  
> a = LeetExp.new(:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> "@",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:letters =\> [[a-z], :insensitive],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ".",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:string =\> ["com", "net", "org", "edu"]  
> )

Another way to do something like this is to use a "compound" regular expression where each part continues where the last one ended. Something like \@\.\* where $1 would be everything up to the @ sign, i.e. the name. $2 would be everything between the @ and the ., i.e. the ISP name. And $3 would be the remainder of the address. The only time something like this would fail would be an address like mine, where the ISP name is worldnet.att rather than just worldnet.

> **···**
>
> > case line  
> > when a  
> > &nbsp;&nbsp;&nbsp;&nbsp;# ...
> > 
> > My idea is to make it logical and human readable. Ruby is a language for humans and UberBeings, and I think this should reflect Ruby's ideas.
> > 
> > Also, was you library a wrapper for underlying PERL RegExp? or was it the whole RegExp engine?
> > 
> > Thanks,  
> > Ari
> > 
> > On Aug 6, 2007, at 10:51 PM, Kenneth McDonald wrote:
> > 
> > > Ari,
> > > 
> > > How serious are you about this? Several years ago I wrote a Python library that treats Python regular  
> > > expressions as semantic, not syntactic, objects, and that has been incredibly useful to me. I've started  
> > > to port it to Ruby, but simply don't have the time. If you do (you're probably looking at a couple of  
> > > weeks of full-time-equivalent hours to do a good job, including decent documentation), I'm happy to pass  
> > > on the Python code, the Ruby code, and give advice and so on.
> > > 
> > > To help you evaluate this, and also as a potential source of ideas in case you do something else, I've  
> > > appended my (probably out of date) intro text to the library at the bottom of this reply.
> > > 
> > > Cheers,  
> > > Ken
> > 
> > --------------------------------------------|  
> > If you're not living on the edge,  
> > then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [21 December 2009 17:29 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/17 "2009-12-21T17:29:58Z")

</div>

Nowadays DFA's are rare because NFA provide more features and you can  
use them to your advantage (i.e. prioritizing by ordering  
alternatives). You can "switch off" backtracking by using atomic  
groups and greedy quantifiers:  
[http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)

Kind regards

robert

> **···**
>
> 2009/12/21 Brian Candler \<b.candler@pobox.com\>:
> 
> > Phlip wrote:
> > 
> > > ```
> > > rx = match\(&#39;foo&#39;\) or match\(&#39;bar&#39;\) \# like /\(foo|bar\)/
> > > 
> > > ```
> > 
> > Aside: you either need to use '||' instead of 'or', or you need extra  
> > parentheses, i.e.
> > 
> > rx = (match('foo') or match('bar'))
> > 
> > otherwise it parses as
> > 
> > (rx = match('foo')) or match('bar')
> > 
> > I don't really have a problem with regexps as they are. Although I'd  
> > like to have more limited, \*true\* regexps, which compile to a DFA and  
> > never backtrack.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard2](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Simon\_Strandgaard2](https://rubytalk.org/u/Simon_Strandgaard2)\
**Post date:** [7 August 2007 13:36 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/18 "2007-08-07T13:36:52Z")

</div>

[snip]

> Anyway -- somewhere out there, though I haven't been able to find it,  
> is a library called Regexp::English by Florian Gross, which provides a  
> kind of English-language wrapper for regexes. I don't know whether  
> it's still in development and/or at a point of usability.

long time ago I wrote a regexp engine 96% compatible with the ruby's,  
at that point in time. Maybe it's useful to somebody?  
[http://raa.ruby-lang.org/project/regexp/](http://raa.ruby-lang.org/project/regexp/)

> **···**
>
> On 8/7/07, dblack@rubypal.com \<dblack@rubypal.com\> wrote:
> 
> --  
> Simon Strandgaard  
> [http://opcoders.com/](http://opcoders.com/)

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://rubytalk.org/u/Kenneth_McDonald)\
**Post date:** [7 August 2007 16:55 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/19 "2007-08-07T16:55:36Z")

</div>

Speaking as someone who has actually written and used (in Python) a more abstract regex library,  
the biggest problem with regular expressions in most languages isn't the syntax, but rather  
the inability to easily compose small REs into larger REs. Which is why so many programs end  
up with huge, unreadable REs. As a small example, it's really nice (and obvious) to be able to say

&nbsp;&nbsp;&nbsp;&nbsp;re3 = re1 + re2

instead of

&nbsp;&nbsp;&nbsp;&nbsp;re3 = "(?:#{re1})(?:#{re2})"

And the advantages go well beyond the convenience illustrated in the above example...

Also, I think that people who are accustomed to regular expressions (or any other DSL) tend  
to forget about the problems with that DSL; the need for newcomers to learn another syntax,  
the inability to use standard language tools with the DSL, and so on.

So, though I've used REs for years, I certainly don't agree with the contention that "REs  
are actually pretty good". RE syntax in RE languages is optimized for quickly entering onetime  
REs on the command line, not for building robust REs that can be easily maintained by  
other programmers. It's the difference between weird, Perl-style variables, and meaningful  
variable names. A good abstract wrapper in Ruby would be very useful.

Ken

Yossef Mendelssohn wrote:

> **···**
>
> > I second (or third, or whatever) the contention that regular  
> > expressions are pretty readable on their own (given some knowledge of  
> > the syntax and good formatting). The thing to keep in mind is that  
> > they're a language of their own. Once you learn the language, you  
> > find you can use it in many a programming language (though there are  
> > some dialectical problems here and there).
> > 
> > --  
> > -yossef

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [7 August 2007 15:15 UTC](https://rubytalk.org/t/alternate-regular-expressions/39716/20 "2007-08-07T15:15:35Z")

</div>

Ari,

Do it!  
excellent project. even if it fails in the long run, or if you pass it off to somebody else.

I like the Rails-like hash-looking idea, of course you would need some ordering, so it would need to be some kind of array or struct, but it is an idea worth toying with.

> **···**
>
> On Aug 7, 2007, at 8:36 AM, Simon Strandgaard wrote:
> 
> > On 8/7/07, dblack@rubypal.com \<dblack@rubypal.com\> wrote:  
> > [snip]
> > 
> > > Anyway -- somewhere out there, though I haven't been able to find it,  
> > > is a library called Regexp::English by Florian Gross, which provides a  
> > > kind of English-language wrapper for regexes. I don't know whether  
> > > it's still in development and/or at a point of usability.
> > 
> > long time ago I wrote a regexp engine 96% compatible with the ruby's,  
> > at that point in time. Maybe it's useful to somebody?  
> > [http://raa.ruby-lang.org/project/regexp/](http://raa.ruby-lang.org/project/regexp/)
> > 
> > --  
> > Simon Strandgaard  
> > [http://opcoders.com/](http://opcoders.com/)

[Next page](https://rubytalk.org/t/alternate-regular-expressions/39716.md?page=2)
