Alternate Regular Expressions?

P

Pit Capitain

2007/8/8 said:
Did you think about something like this (attached)? This is just a
raw hack to illustrate a possible way to do it.
Quote:

mail_addr = TextualRegexp.new do
anchor :beginning

group :capturing do
at_least_once { any "a-z" }
end

literal "@"

repeat 1..4 do
at_least_once { any "a-z" }
literal "."
end

any %w{com edu org}
end

I like this! A readable DSL for regular expressions.

Regards,
Pit
 
R

Roger Pack

rather
I agree with this and that's why I have the following add-on in my
standard lib:

class Regexp
def +(other_regex)
...
end
end

Note also that you can do

re3 = /#{re1}#{re2}/ which fits my needs pretty well.

-r
 
M

Marnen Laibow-Koser

Ari said:
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,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed) 
 
B

Brian Candler

Phlip said:
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.

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.
 
R

Robert Klemme

2009/12/21 Brian Candler said:
Aside: you either need to use '||' instead of 'or', or you need extra
parentheses, i.e.

=A0 rx =3D (match('foo') or match('bar'))

otherwise it parses as

=A0(rx =3D 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.

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

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Albert Schlef

Ari said:
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 http://weitz.de/cl-ppcre/

The regexp string...

(?:abc){3,5}

...is equal to the following data structure:

:)GREEDY-REPETITION 3 5 :)GROUP "abc"))

which, in Ruby, looks like:

[: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Polymorphic Code 31
Clearing the RAM 7
SCP Ruby 2
Recursive regular expressions in Ruby? 4
AVL Tree 4
Regular Expressions 4
Keylogging in Ruby 0
Rubinius on Mac PPC 3

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top