8 Replies Latest reply: Oct 30, 2007 4:48 AM by 807603 RSS

    string pattern

    807603
      hi,
      can any one help me on regex, how to check string has combination of alphabets, numerals and '_' sign.

      Is [a-zA-Z][\w\.-]*[a-zA-Z0-9] this one is correct
        • 1. Re: string pattern
          791266
          victorygls wrote:
          hi,
          can any one help me on regex, how to check string has combination of alphabets, numerals and '_' sign.

          Is [a-zA-Z][\w\.-]*[a-zA-Z0-9] this one is correct
          No it isn't.

          Post a few valid strings, and some invalid strings.
          • 2. Re: string pattern
            807603
            valid strings are mahesh_kumar, adcede, ravi223
            invalid strings are mhesh.kumar, adf@dd, ravi-332
            • 3. Re: string pattern
              791266
              victorygls wrote:
              valid strings are mahesh_kumar, adcede, ravi223
              invalid strings are mhesh.kumar, adf@dd, ravi-332
              is _123 also valid?
              is a_1_3_bcA valid?
              • 4. Re: string pattern
                807603
                its valid, its combination of alphabets, numerals and "_" only, other than any special characters its invalid
                • 5. Re: string pattern
                  791266
                  victorygls wrote:
                  its valid, its combination of alphabets, numerals and "_" only, other than any special characters its invalid
                  Uset this pattern in that case:
                  "\\w+"
                  Edited by: kajbj on Oct 30, 2007 10:42 AM
                  • 6. Re: string pattern
                    807603
                    Your first guess was near the truth, but you seem a bit confused how regexp works.

                    So, a quick tutorial:

                    You can match any character (excluding newline):
                    .
                    You can match any letter with:
                    \w
                    or
                    [a-zA-Z]
                    You can match any digit with:
                    \d
                    or
                    [0-9]
                    You can match a character one or more times:
                    \d+
                    You can match a character zero or one times:
                    \d?
                    You can match a character zero or many times:
                    \d*
                    You can match a whitespace character (including newline):
                    \s
                    So your pattern would be very easy. We need not to escape the character '_', since it is not a special character:
                    [\d\w_]+
                    • 7. Re: string pattern
                      791266
                      stefan.xx wrote:
                      Your first guess was near the truth, but you seem a bit confused how regexp works.
                      So your pattern would be very easy. We need not to escape the character '_', since it is not a special character:
                      [\d\w_]+
                      It's easier than that. \\w is the same as a character group with a-zA-Z_0-9

                      So the answer is
                      \w+
                      • 8. Re: string pattern
                        807603
                        Ah, yes of course.

                        Thank you for correcting. It was me who was confused there for a moment. :)