Advanced Regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christ

    Advanced Regex

    Hi there,

    i'm trying to make a regex, but it ain't working.
    In just one regex expression I want to check a password that must meet
    following requirements:
    - at least 6 characters long
    - at least one letter (doesn't matter where in te string)
    - at least one number (doesn't matter where in te string)
    (those are no prob)
    - excluding 0, o, O, i, I, l, L, 1 (to prevent confusion about
    password chars when font is for ex. Arial)

    Can anyone help?

    Thanks

    Christ.
  • Thomas 'PointedEars' Lahn

    #2
    Re: Advanced Regex

    Christ wrote:[color=blue]
    > i'm trying to make a regex, but it ain't working.[/color]

    "It doesn't work" is a useless error *description*.
    What have you already tried? What errors have occured
    on which line?


    PointedEars

    Comment

    • Geir Klemetsen

      #3
      Re: Advanced Regex


      "Christ" <christophethel en@hotmail.com> skrev i melding
      news:36ec41f6.0 310160118.24927 5c7@posting.goo gle.com...[color=blue]
      > Hi there,
      >
      > i'm trying to make a regex, but it ain't working.
      > In just one regex expression I want to check a password that must meet
      > following requirements:
      > - at least 6 characters long
      > - at least one letter (doesn't matter where in te string)
      > - at least one number (doesn't matter where in te string)
      > (those are no prob)
      > - excluding 0, o, O, i, I, l, L, 1 (to prevent confusion about
      > password chars when font is for ex. Arial)
      >
      > Can anyone help?
      >
      > Thanks
      >
      > Christ.[/color]

      Why use regex? Isn't there much more easier to just traverse the string to
      see if you can find a number, a letter and an "if password.length > 5 { " to
      check the length ? I would do it that way.


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Advanced Regex

        christophethele n@hotmail.com (Christ) writes:
        [color=blue]
        > In just one regex expression I want to check a password that must meet
        > following requirements:[/color]

        Why just one regular expression? Sure, anything you can make with more
        than one regular expression can be made with just one, but the size of
        the one can easily be the product of the sizes of the many (and
        shorthands make it worse, since they typically apply to simple
        expressions more than complex ones)
        [color=blue]
        > - at least 6 characters long[/color]

        input.length >= 6
        [color=blue]
        > - at least one letter (doesn't matter where in te string)[/color]

        (/[a-hj-kmnp-z]/i).test(input)
        [color=blue]
        > - at least one number (doesn't matter where in te string)[/color]

        (/[2-9]/).test(input)
        [color=blue]
        > (those are no prob)
        > - excluding 0, o, O, i, I, l, L, 1 (to prevent confusion about
        > password chars when font is for ex. Arial)[/color]

        Combining these in one expression will give you a *huge* expression,
        I can tell you that without trying.
        There is one shorthand that can help you, though: look-ahead. It
        only exist in recent versions of Javascript, though.

        /^(?=.*[2-9])(?=.*[a-hj-kmnp-z]).{6,}/i
        ^^^^^^^^^^^ look-ahead contains at least one of 2-9
        ^^^^^^^^^^^^^^^ ^^^^ look-ahead contains a letter, not i,l,o
        ^^^^^ at least six characters

        It seems to me that IE has a problem with positive lookahead:
        /^(?=.+4)123/.test("1234")
        This test should give true. In IE it gives false. However,
        /^(?=.+4)123/.test("12345")
        does give true. Changing + to * just makes it worse.

        Some times, using just one regular expression is shooting your own foot.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Advanced Regex

          Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
          [color=blue]
          > /^(?=.*[2-9])(?=.*[a-hj-kmnp-z]).{6,}/i[/color]

          Doh. That doesn't prevent the remaining characters from being o, i, l,
          1, or 0. For that, do:
          /^(?=.*[2-9])(?=.*[a-hj-kmnp-z])[^oil01]{6,}/i

          However, that also allows characters like |, !, ó, ò, ô, etc. These might
          also be confuzed with each other. If you just want digits and characters,
          change [^oil01] to [2-9a-hjkmnp-z].

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: Advanced Regex

            JRS: In article <ad81quaq.fsf@h otpop.com>, seen in
            news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
            posted at Thu, 16 Oct 2003 13:46:37 :-[color=blue]
            >[color=green]
            >> - at least 6 characters long[/color]
            >
            > input.length >= 6
            >[color=green]
            >> - at least one letter (doesn't matter where in te string)[/color]
            >
            > (/[a-hj-kmnp-z]/i).test(input)
            >[color=green]
            >> - at least one number (doesn't matter where in te string)[/color]
            >
            > (/[2-9]/).test(input)
            >[/color]


            I'd do
            (/^[a-hj-kmnp-z2-9]{6,}$/i).test(input)
            instead of the first; it also checks that all characters are reasonable
            - " A 3 " is in accordance with the stated - presumed homework -
            requirement but is not a good password.

            The later tests can use [a-z] and \d then.

            However, passwords should not appear on the screen when typing, so
            screen font does not matter; and if they have to be communicated in
            writing that can be done in a sensible font.

            Since L can, he says, be confused, presumably case does not matter; in
            that case, the restriction to 31 of 36 alphanumerics reduces security by
            about 60%.

            The minus between j & k seems unnecessary.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • rh

              #7
              Re: Advanced Regex

              Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<65ipqu0r. fsf@hotpop.com> ...[color=blue]
              > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
              >[color=green]
              > > /^(?=.*[2-9])(?=.*[a-hj-kmnp-z]).{6,}/i[/color]
              >
              > Doh. That doesn't prevent the remaining characters from being o, i, l,
              > 1, or 0. For that, do:
              > /^(?=.*[2-9])(?=.*[a-hj-kmnp-z])[^oil01]{6,}/i
              >
              > However, that also allows characters like |, !, ó, ò, ô, etc. These might
              > also be confuzed with each other. If you just want digits and characters,
              > change [^oil01] to [2-9a-hjkmnp-z].
              >
              > /L[/color]

              Seems you intended to add a $ anchor to really ensure no extraneous
              characters at the end of the string:

              /^(?=.*[2-9])(?=.*[a-hj-kmnp-z])[2-9a-hj-kmnp-z]{6,}$/i


              However, while Opera 7.11 appears to handle this correctly, Netscape
              7.1 (as well as IE) has problems with lookahead. So for something that
              should be equivalent and stand a chance of working generally:

              /[a-z]\d|\d[a-z]/i.test(pwStr)
              && /^[2-9a-hj-kmnp-z]{6,}$/i.test(pwStr)

              Comment

              • rh

                #8
                Re: Advanced Regex

                Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<5SRSI2RgU vj$Ewqc@merlyn. demon.co.uk>...[color=blue]
                > JRS: In article <ad81quaq.fsf@h otpop.com>, seen in
                > news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                > posted at Thu, 16 Oct 2003 13:46:37 :-[/color]
                [color=blue]
                > The minus between j & k seems unnecessary.[/color]

                Right, it is. Probably a typo in placing the dash -- [a-h-jkmnp-z] was
                the sequence intended, I believe, to eliminate i/I as per the OP.

                Comment

                • rh

                  #9
                  Re: Advanced Regex

                  codeHanger@yaho o.ca (rh) wrote in message news:<290e65a0. 0310161747.7adc 706a@posting.go ogle.com>...[color=blue]
                  > Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<5SRSI2RgU vj$Ewqc@merlyn. demon.co.uk>...[color=green]
                  > > JRS: In article <ad81quaq.fsf@h otpop.com>, seen in
                  > > news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                  > > posted at Thu, 16 Oct 2003 13:46:37 :-[/color]
                  >[color=green]
                  > > The minus between j & k seems unnecessary.[/color]
                  >
                  > Right, it is. Probably a typo in placing the dash -- [a-h-jkmnp-z] was
                  > the sequence intended, I believe, to eliminate i/I as per the OP.[/color]

                  And then again on this one perhaps it would have been better to say
                  "Right it is.", or better yet nothing at all :-).

                  Comment

                  • rh

                    #10
                    Re: Advanced Regex

                    Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<ad81quaq. fsf@hotpop.com> ...[color=blue]
                    > christophethele n@hotmail.com (Christ) writes:
                    >[color=green]
                    > > In just one regex expression I want to check a password that must meet
                    > > following requirements:[/color]
                    >
                    >
                    > /^(?=.*[2-9])(?=.*[a-hj-kmnp-z]).{6,}/i
                    > ^^^^^^^^^^^ look-ahead contains at least one of 2-9
                    > ^^^^^^^^^^^^^^^ ^^^^ look-ahead contains a letter, not i,l,o
                    > ^^^^^ at least six characters
                    >
                    > It seems to me that IE has a problem with positive lookahead:
                    > /^(?=.+4)123/.test("1234")
                    > This test should give true. In IE it gives false. However,
                    > /^(?=.+4)123/.test("12345")
                    > does give true. Changing + to * just makes it worse.
                    >[/color]

                    Lookahead in a RegExp is supposed to be a zero-width assertion. In
                    addition to the above problems, IE 6 "eats characters" in the
                    lookahead. Netscape 6.1 and Mozilla 5/1.2b, by some coincidence, do
                    the same.

                    Opera 7.11 gets it right (and appears to match Perl behaviour) in a
                    limited number of tests performed.

                    So it seems that lookahead in a js RegExp is something that should be
                    strictly avoided, unless one is authoring for a very specific js
                    environment that can properly support it.

                    ..\rh

                    Comment

                    Working...