in reply to Regular Expression Question

If multiple regexes are acceptable, this'll get you a little closer to what you want, I think. The first regex in the if statement matches the alphanumerics and commas, but not leading/trailing commas; the second regex excludes consecutive commas.

use warnings; use strict; while ( <DATA> ) { print "$1\n" if /^(\w+[\w,]+\w+)$/ && !/,,/; } __DATA__ !@#$as3dfa ,sdfas3df, asd3fsa,,a3sdf as3df,asdf3,3asdf,asd3f sad3fasdjasdfkasdfklas3jf 3sad3fasdjasdfkasdfklas3jf 3sad3fasdjasdfkasdfklas3jf3

Output is:

as3dfa sdfas3df as3df,asdf3,3asdf,asd3f sad3fasdjasdfkasdfklas3jf 3sad3fasdjasdfkasdfklas3jf 3sad3fasdjasdfkasdfklas3jf3

Replies are listed 'Best First'.
Re: Re: Regular Expression Question
by TASdvlper (Monk) on Dec 04, 2003 at 20:06 UTC
    print "$1\n" if /^(\w+[\w,]+\w+)$/ && !/,,/;

    So, if I wanted to check for more than 2 consecutive commas (I should have mentioned that in the original post) I would do the following:

    print "$1\n" if /^(\w+[\w,]+\w+)$/ && !/,{2,}/;
      Either would work-- !/,,/ exludes strings containing 2 consecutive commas, and !/,{2,}/ excludes strings containing 2-or-more consecutive commas, which amounts to pretty much the same thing for what you want, if I've understood you correctly.
        Oop, sorry-- misread your question. If you wanted to check for more than 2 commas, you'd want to use either:
        /(\w+[\w,]+\w+)/ && !/,,,/

        or this:

        if /^(\w+[\w,]+\w+)$/ && !/,{3,}/;
Re: Re: Regular Expression Question
by Not_a_Number (Prior) on Dec 04, 2003 at 21:40 UTC

    Problem is, this doesn't work for very short strings (2 or fewer characters). Add these:

    __DATA__ a bc

    dave