By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

Listing all regexs possibilities

P: 7
I have some regex lists, and I need to print all possibilities from them.

Like that:

print abac[ao]

that would print:

abaca
abaco

Can this be possible?
Apr 2 '08 #1
Share this Question
Share on Google+
11 Replies


eWish
Expert 100+
P: 971
I would say yes. What have you tried?

--Kevin
Apr 2 '08 #2

P: 7
Yeah, but it didn't work:

$ perl -e 'print "abac[ao]\n"'
abac[ao]
Apr 2 '08 #3

eWish
Expert 100+
P: 971
If you want to do 'a' or 'o' then you would need abc[a|o].

Expand|Select|Wrap|Line Numbers
  1. my @array = qw(abca abcd abco abca abco abci abcf abca);
  2.  
  3. foreach (grep/abc[a|o]/, @array) {
  4.     print $_, "\n";
  5. }
--Kevin
Apr 2 '08 #4

P: 7
But I don't want to supply words...I'm looking for something like this:

print "abc[a|o]";

or

print "test?[a-z]"

and go on...

there is a way to do that?
Apr 3 '08 #5

eWish
Expert 100+
P: 971
I used a list as an example. Can you show me 10 lines of expected output, because I am having trouble understanding what you are wanting to do I guess. Also, where is your data from from?

Edit: Are you looking for something like this:
Expand|Select|Wrap|Line Numbers
  1. print join("\n", ('testa ..testz'));
Output:
Expand|Select|Wrap|Line Numbers
  1. testa
  2. testb
  3. testc
  4. testd
  5. and so on to testz
--Kevin
Apr 3 '08 #6

Ganon11
Expert 2.5K+
P: 3,652
The OP has a list of regular expressions, and wants to be able to print all possible patterns the regex will match. So if he had a simple one like /\d/, it would print each string that the regex would match - namely, 0, 1, 2, etc. etc.
Apr 3 '08 #7

P: 7
Ganon11: that's exacly what I'm trying to do.
Apr 3 '08 #8

eWish
Expert 100+
P: 971
I have never done it, but I am sure that it is possible.

--Kevin
Apr 4 '08 #9

Ganon11
Expert 2.5K+
P: 3,652
You can probably do it with a recursive function. Imagine this:

You are building a word step by step when you encounter a special character - be it a \d, or a [a-z], or whatever. You then loop over the possible matches, calling the function with a partial string (your word so far, plus the character your loop is on) and the pattern.

When there are no more special cases, you print out the word and return, and let recursion work its magic.

Note: as soon as you have a modifier like + or *, you will have an infinite number of possibilities (e.g. /\d+/ will match 1, 11, 111, 1111, ..., 11111111111111111...., etc. etc. So you need to make sure that doesn't happen.
Apr 4 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
I have some regex lists, and I need to print all possibilities from them.

Like that:

print abac[ao]

that would print:

abaca
abaco

Can this be possible?
Possible? Yes. Easy and automatic? No. Perl will not expand your regexp character class automatically and print all possible permutations, you will pretty much have to hard code that yourself.
Apr 4 '08 #11

P: 7
It's ok guys, I was hoping for a easy solution. It's better forget it. Thanks!
Apr 9 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.