Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > matches -> regexp ?

Reply
Thread Tools

matches -> regexp ?

 
 
ako...
Guest
Posts: n/a
 
      12-12-2005
Hello,

this is not exactly a ruby problem, i hope people will not consider
this spamming.

does anyone know of any reference or an algorithm to build a regular
expression (a finite automaton, in general) that would match a given
set of strings? i of course mean a regexp that would match just the
given strings, and not any other. so solutions like /^.*$/ are not
acceptable

thanks
konstantin

 
Reply With Quote
 
 
 
 
Robert Retzbach
Guest
Posts: n/a
 
      12-12-2005
ako... schrieb:

>Hello,
>
>this is not exactly a ruby problem, i hope people will not consider
>this spamming.
>
>does anyone know of any reference or an algorithm to build a regular
>expression (a finite automaton, in general) that would match a given
>set of strings? i of course mean a regexp that would match just the
>given strings, and not any other. so solutions like /^.*$/ are not
>acceptable
>
>thanks
>konstantin
>
>
>
>
>

nice challenge, reminds me a bit of the 4th rubyquiz :>


 
Reply With Quote
 
 
 
 
Edward Faulkner
Guest
Posts: n/a
 
      12-12-2005
--o0ZfoUVt4BxPQnbU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

> ako... schrieb:
> >does anyone know of any reference or an algorithm to build a regular
> >expression (a finite automaton, in general) that would match a given
> >set of strings? i of course mean a regexp that would match just the
> >given strings, and not any other. so solutions like /^.*$/ are not
> >acceptable


This is essentially an information compression problem. There's
always a trivial solution:

s = %w{find only these strings}
r = Regexp.new(s.map {|w| '\A' + w + '\Z'}.join('|'))

The _real_ problem is finding the shortest possible regexp.

--o0ZfoUVt4BxPQnbU
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD4DBQFDneKDnhUz11p9MSARAhZdAJikR9LxgvRKe+HQx+NM/iFPTQPDAKCg7yyF
k9ZvZV/M0aud/vCU9NxSLw==
=EgnI
-----END PGP SIGNATURE-----

--o0ZfoUVt4BxPQnbU--


 
Reply With Quote
 
ako...
Guest
Posts: n/a
 
      12-12-2005
well, right, that is what i mean.

 
Reply With Quote
 
ako...
Guest
Posts: n/a
 
      12-12-2005
i suspect that from your trivial solution we can arrive at the final
solution by just performing a DFA minimization. the same thing that lex
and yacc do when they generate code. so i guess i have answered my own
question. thanks for helping me. your trivial solution just made my
brain work.

konstantin

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-13-2005
ako... wrote:
> i suspect that from your trivial solution we can arrive at the final
> solution by just performing a DFA minimization. the same thing that
> lex and yacc do when they generate code. so i guess i have answered
> my own question. thanks for helping me. your trivial solution just
> made my brain work.


Another option is to build up a tree and create the RX from that. I once
wrote that:

14:12:23 [c]: ( echo foo; echo bar; echo band ) | create-rx.rb
(?:ba(?:nd|r)|foo)

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace random matches of regexp gry Python 4 09-08-2011 09:19 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
[Regexp] Howto capture all matches of a single group ersin.er@gmail.com Java 3 10-03-2005 10:11 AM
multiple regexp matches Kevin Howe Ruby 27 08-24-2004 02:30 PM
Please help with regexp - finding all matches? Boris Pelakh Perl 3 04-08-2004 08:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57