Listing all regexs possibilities

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bugmenot2
    Banned
    New Member
    • Apr 2008
    • 7

    Listing all regexs possibilities

    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?
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    I would say yes. What have you tried?

    --Kevin

    Comment

    • bugmenot2
      Banned
      New Member
      • Apr 2008
      • 7

      #3
      Yeah, but it didn't work:

      $ perl -e 'print "abac[ao]\n"'
      abac[ao]

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        If you want to do 'a' or 'o' then you would need abc[a|o].

        [CODE=perl]my @array = qw(abca abcd abco abca abco abci abcf abca);

        foreach (grep/abc[a|o]/, @array) {
        print $_, "\n";
        }[/CODE]

        --Kevin

        Comment

        • bugmenot2
          Banned
          New Member
          • Apr 2008
          • 7

          #5
          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?

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            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:
            [CODE=perl]print join("\n", ('testa ..testz'));[/CODE]
            Output:
            Code:
            testa
            testb
            testc
            testd
            and so on to testz
            --Kevin

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

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

              Comment

              • bugmenot2
                Banned
                New Member
                • Apr 2008
                • 7

                #8
                Ganon11: that's exacly what I'm trying to do.

                Comment

                • eWish
                  Recognized Expert Contributor
                  • Jul 2007
                  • 973

                  #9
                  I have never done it, but I am sure that it is possible.

                  --Kevin

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    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, ..., 111111111111111 11...., etc. etc. So you need to make sure that doesn't happen.

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by bugmenot2
                      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.

                      Comment

                      • bugmenot2
                        Banned
                        New Member
                        • Apr 2008
                        • 7

                        #12
                        It's ok guys, I was hoping for a easy solution. It's better forget it. Thanks!

                        Comment

                        Working...