# Reverse RegEx or How to write a KeywordGenerator for a given

**URL:** <https://www.ruby-forum.com/t/reverse-regex-or-how-to-write-a-keywordgenerator-for-a-given/127701>\
**Category:** Ruby\
**Created:** [January 31, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/reverse-regex-or-how-to-write-a-keywordgenerator-for-a-given/127701 "2008-01-31T15:17:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![muescha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@muescha](https://www.ruby-forum.com/u/muescha)\
**Post date:** [January 31, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/reverse-regex-or-how-to-write-a-keywordgenerator-for-a-given/127701/1 "2008-01-31T15:17:05Z")

</div>

im new to ruby and have not the deep knowledge of all the possible ways  
to get a solution. but for to the sequence generator im very surprised  
of the clean short ruby solution…

My Goal:  
write a parser for some patterns to generate some keywords from it.

RegEx?  
i can have some regex to test some text, but is there a reverse way:  
generate from a given regex all possible mutations for it?

what i found until now?  
i have found a sequence generator  
[http://snippets.dzone.com/posts/show/3332](http://snippets.dzone.com/posts/show/3332)  
witch i can give this pattern:

x = [[‘abc’],[‘’,‘-’],[‘def’],[‘’,‘n’],[‘123’,‘456’]]

and get this permutations/sequences:

abcdef123  
abcdef456  
abcdefn123  
abcdefn456  
abc-def123  
abc-def456  
abc-defn123  
abc-defn456

but i will not will not write this difficult arrays. i like a light  
pattern syntax to generate this arrays and permutations

for instance for the given pattern:

“abc-defn?[123|456]”

i will generate this array:  
x = [[‘abc’],[‘’,‘-’],[‘def’],[‘’,‘n’],[‘123’,‘456’]]

some rules:  
? =\> means one or not  
| =\> mens OR  
[] =\> braces  
“-” =\> replace with this “-?”  
“Ã¤” “Ã¶” “Ã¼” “ÃŸ” =\> automaticly goes to “[Ã¤|ae]” or “[Ã¼|ue]” or “[Ã¶|oe]”  
or “[ss]”  
in combination with () and some $1 $2 i can generate some RegEx like  
substitutions  
with ${varname} i can include some predefined big patternlists

i like also use recursive patterns:

“[123[1|2]|45]” etc.

Solutions?  
i can cut the strings with some string replace etc. but i think this is  
not the good ruby way.

is a DSL parser/generator the right way for it? which one is the best  
for it (ParseTree/TreeTop/GhostWheel/

can someone give me some routing directions where i can look for the  
best ruby way for my idea?

---

<div class="post-metadata">

**Author:** ![muescha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@muescha](https://www.ruby-forum.com/u/muescha)\
**Post date:** [January 31, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/reverse-regex-or-how-to-write-a-keywordgenerator-for-a-given/127701/2 "2008-01-31T15:27:07Z")

</div>

On Jan 31, 2008, at 8:16 AM, Mi Ni wrote:

> RegEx?  
> i can have some regex to test some text, but is there a reverse way:  
> generate from a given regex all possible mutations for it?

Sure, for at least some subsets of regular expressions:

[http://www.rubyquiz.com/quiz143.html](http://www.rubyquiz.com/quiz143.html)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![muescha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@muescha](https://www.ruby-forum.com/u/muescha)\
**Post date:** [January 31, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/reverse-regex-or-how-to-write-a-keywordgenerator-for-a-given/127701/3 "2008-01-31T15:53:39Z")

</div>

> Sure, for at least some subsets of regular expressions:
> 
> [Ruby Quiz - Reverse the Polarity (#143)](http://www.rubyquiz.com/quiz143.html)
> 
> James Edward G. II

i look quick over the quiz. i think there some solutions for it 🙂  
i have to go deeper in the results.

a solution for my german umlauts (Ã¤Ã¼Ã¶ÃŸ) and “-” i can implement with  
string-replaces

so i have to look also for:

- a solution for my ? operator
- a nested-syntax solution … maybe this is in the quiz solutions…
