Another Reg Exp problem

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

    Another Reg Exp problem

    Alright, I don't have time to learn all the intricacies of regular
    expressions, but what I want to do is match a string that has:

    1) only digits
    2) may have a comma
    3) must not start or end with a comma

    So basically, a string of numbers separated with commas. valid input
    could be

    123
    123,124
    123,125,125
    1,2,3
    1,12,123

    Invalid would be

    123,
    ,123
    123 124 125
    123abc

    TIA!
  • kaeli

    #2
    Re: Another Reg Exp problem

    In article <5db363e0.04052 40946.41baabd8@ posting.google. com>,
    larrybud2002@ya hoo.com enlightened us with...[color=blue]
    > Alright, I don't have time to learn all the intricacies of regular
    > expressions, but what I want to do is match a string that has:
    >
    > 1) only digits
    > 2) may have a comma
    > 3) must not start or end with a comma
    >
    > So basically, a string of numbers separated with commas. valid input
    > could be
    >[/color]

    Try
    /^\d[,\d]*$/

    --
    --
    ~kaeli~
    Murphy's Law #2030: If at first you don't succeed, destroy
    all evidence that you tried.



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Another Reg Exp problem

      kaeli <tiny_one@NOSPA M.comcast.net> writes:
      [color=blue][color=green]
      >> 1) only digits
      >> 2) may have a comma
      >> 3) must not start or end with a comma[/color][/color]
      [color=blue]
      > Try
      > /^\d[,\d]*$/[/color]

      This can end with a comma. You probably mean
      /^\d+(,\d+)*$/

      Hmm. In fact, the original poster didn't say that the string must be
      non-empty, so a more correct solution would be:
      /^(\d+(,\d+)*)?$/

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Lee

        #4
        Re: Another Reg Exp problem

        Larry Bud said:[color=blue]
        >
        >Alright, I don't have time to learn all the intricacies of regular
        >expressions, but what I want to do is match a string that has:
        >
        >1) only digits
        >2) may have a comma
        >3) must not start or end with a comma
        >
        >So basically, a string of numbers separated with commas. valid input
        >could be
        >
        >123
        >123,124
        >123,125,125
        >1,2,3
        >1,12,123
        >
        >Invalid would be
        >
        >123,
        >,123
        >123 124 125
        >123abc[/color]

        If there must be at least two digits, then it's easy:
        /^\d+[,\d]*\d$/

        That's a digit at the beginning, any number of [comma or digit],
        followed by a digit at the end.

        If a single digit is also valid, then it's only a little more complicated:

        /(^\d+[,\d]*\d$)|(^\d$)/

        That's the same as above OR just one digit.

        Comment

        • Lee

          #5
          Re: Another Reg Exp problem

          kaeli said:[color=blue]
          >
          >In article <5db363e0.04052 40946.41baabd8@ posting.google. com>,
          >larrybud2002@y ahoo.com enlightened us with...[color=green]
          >> Alright, I don't have time to learn all the intricacies of regular
          >> expressions, but what I want to do is match a string that has:
          >>
          >> 1) only digits
          >> 2) may have a comma
          >> 3) must not start or end with a comma
          >>
          >> So basically, a string of numbers separated with commas. valid input
          >> could be
          >>[/color]
          >
          >Try
          >/^\d[,\d]*$/[/color]

          That could end with a comma

          Comment

          • kaeli

            #6
            Re: Another Reg Exp problem

            In article <3c5pn0uv.fsf@h otpop.com>, lrn@hotpop.com enlightened us
            with...[color=blue]
            > kaeli <tiny_one@NOSPA M.comcast.net> writes:
            >[color=green][color=darkred]
            > >> 1) only digits
            > >> 2) may have a comma
            > >> 3) must not start or end with a comma[/color][/color]
            >[color=green]
            > > Try
            > > /^\d[,\d]*$/[/color]
            >
            > This can end with a comma. You probably mean
            > /^\d+(,\d+)*$/
            >[/color]

            So close, and yet so far. :)
            I'll get regex eventually.


            --
            --
            ~kaeli~
            Support your local medical examiner: die strangely!



            Comment

            • Larry Bud

              #7
              Re: Another Reg Exp problem

              Lee <REM0VElbspamtr ap@cox.net> wrote in message news:<c8te5r01r tl@drn.newsguy. com>...[color=blue]
              > Larry Bud said:[color=green]
              > >
              > >Alright, I don't have time to learn all the intricacies of regular
              > >expressions, but what I want to do is match a string that has:
              > >
              > >1) only digits
              > >2) may have a comma
              > >3) must not start or end with a comma
              > >
              > >So basically, a string of numbers separated with commas. valid input
              > >could be
              > >
              > >123
              > >123,124
              > >123,125,125
              > >1,2,3
              > >1,12,123
              > >
              > >Invalid would be
              > >
              > >123,
              > >,123
              > >123 124 125
              > >123abc[/color]
              >
              > If there must be at least two digits, then it's easy:
              > /^\d+[,\d]*\d$/
              >
              > That's a digit at the beginning, any number of [comma or digit],
              > followed by a digit at the end.
              >
              > If a single digit is also valid, then it's only a little more complicated:
              >
              > /(^\d+[,\d]*\d$)|(^\d$)/
              >
              > That's the same as above OR just one digit.[/color]

              Damn, I DID forget to add that it cannot be empty. Will this work for it as well?

              What if someone enters

              1,,23

              There shouldn't be two commas in a row.

              Comment

              • Dr John Stockton

                #8
                Re: Another Reg Exp problem

                JRS: In article <5db363e0.04052 40946.41baabd8@ posting.google. com>, seen
                in news:comp.lang. javascript, Larry Bud <larrybud2002@y ahoo.com> posted
                at Mon, 24 May 2004 10:46:58 :[color=blue]
                >Alright, I don't have time to learn all the intricacies of regular
                >expressions, but what I want to do is match a string that has:
                >
                >1) only digits
                >2) may have a comma
                >3) must not start or end with a comma
                >
                >So basically, a string of numbers separated with commas.[/color]

                And non-empty.

                So you want a digit, followed by any number, including zero, of comma-
                followed-by-at-least-one-digit - and nothing else.

                OK = /^(\d+)(,\d+)*$/.test(S)

                Tested in
                <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>
                <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>

                OK = /^(\d+,)*(\d+)$/.test(S)

                should also do. In each case, the parentheses around just \d+ are
                superfluous.

                --
                © 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> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Another Reg Exp problem

                  Larry Bud wrote:
                  [color=blue]
                  > Lee <REM0VElbspamtr ap@cox.net> wrote:[color=green]
                  >> Larry Bud said:[color=darkred]
                  >>> Alright, I don't have time to learn all the intricacies of regular
                  >>> expressions, but what I want to do is match a string that has:
                  >>>
                  >>> 1) only digits
                  >>> 2) may have a comma
                  >>> 3) must not start or end with a comma
                  >>> [...][/color]
                  >>
                  >> If there must be at least two digits, then it's easy: /^\d+[,\d]*\d$/
                  >> [...]
                  >> If a single digit is also valid, then it's only a little more
                  >> complicated:
                  >>
                  >> /(^\d+[,\d]*\d$)|(^\d$)/
                  >>
                  >> That's the same as above OR just one digit.[/color]
                  >
                  > Damn, I DID forget to add that it cannot be empty. Will this work for it
                  > as well?[/color]

                  No.
                  [color=blue]
                  > What if someone enters
                  >
                  > 1,,23[/color]

                  It would be accepted using the above RegExp. /\d+/ matches "1",
                  /[,\d]*/ matches ",,2" and the final /\d/ matches "3" then.
                  [color=blue]
                  > There shouldn't be two commas in a row.[/color]

                  I think you are looking for /^(\d(,\d)?)+$/. Could be written as
                  /^(\d,\d|\d)+$/ as well. It remains to be discussed what of them
                  is more efficient.


                  HTH

                  PointedEars

                  Comment

                  Working...