RegExp Again!

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

    #1

    RegExp Again!

    Using Regular Expression, I want to ensure that users enter either a 2-
    digit or a 3-digit whole number in a TextBox. This is how I framed the
    ValidationExpre ssion in the RegularExpressi onValidator:

    --------------------------------------------------------------------------------
    <asp:TextBox ID="txt1" runat="server"/>
    <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
    Display="dynami c" ErrorMessage="I nvalid Text"
    ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
    --------------------------------------------------------------------------------

    Suppose a user enters 16 & 216 in the TextBox. As expected, both the
    numbers return True but if I just reverse the ValidationExpre ssion
    i.e. change the ValidationExpre ssion from

    [0-9]{3}|[0-9]{2}

    to

    [0-9]{2}|[0-9]{3}

    & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
    but 216 strangely evaluates to False!

    After a plethora of trial & error methods, I realized that if the
    second ValidationExpre ssion (the one which evaluates 216 to False) is
    enclosed in brackets & a $ sign is appended at the end of the
    expression like this

    ([0-9]{2}|[0-9]{3})$

    then 216 correctly evaluates to True (& so does 16).

    What I couldn't figure out is the logic behind the expression when it
    is wrapped in brackets & a $ sign is appended at the end! Can someone
    please explain me this? What more work does the edited
    ValidationExpre ssion do to make 216 evaluate to True?

    I am aware that $ means the end of a string.

    Thanks,

    Ron
  • marss

    #2
    Re: RegExp Again!

    On 28 âÅÒ, 05:45, RN1 <r...@rediffmai l.comwrote:
    Using Regular Expression, I want to ensure that users enter either a 2-
    digit or a 3-digit whole number in a TextBox. This is how I framed the
    ValidationExpre ssion in the RegularExpressi onValidator:
    >
    --------------------------------------------------------------------------------
    <asp:TextBox ID="txt1" runat="server"/>
    <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
    Display="dynami c" ErrorMessage="I nvalid Text"
    ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
    --------------------------------------------------------------------------------
    There is a less complex solution:
    ValidationExpre ssion="\d{2,3}"

    Regards,
    Mykola

    Comment

    • Lloyd Sheen

      #3
      Re: RegExp Again!


      "RN1" <rn5a@rediffmai l.comwrote in message
      news:e4dbd317-a37d-4632-8786-d05581b0199b@n5 8g2000hsf.googl egroups.com...
      Using Regular Expression, I want to ensure that users enter either a 2-
      digit or a 3-digit whole number in a TextBox. This is how I framed the
      ValidationExpre ssion in the RegularExpressi onValidator:
      >
      --------------------------------------------------------------------------------
      <asp:TextBox ID="txt1" runat="server"/>
      <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
      Display="dynami c" ErrorMessage="I nvalid Text"
      ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
      --------------------------------------------------------------------------------
      >
      Suppose a user enters 16 & 216 in the TextBox. As expected, both the
      numbers return True but if I just reverse the ValidationExpre ssion
      i.e. change the ValidationExpre ssion from
      >
      [0-9]{3}|[0-9]{2}
      >
      to
      >
      [0-9]{2}|[0-9]{3}
      >
      & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
      but 216 strangely evaluates to False!
      >
      After a plethora of trial & error methods, I realized that if the
      second ValidationExpre ssion (the one which evaluates 216 to False) is
      enclosed in brackets & a $ sign is appended at the end of the
      expression like this
      >
      ([0-9]{2}|[0-9]{3})$
      >
      then 216 correctly evaluates to True (& so does 16).
      >
      What I couldn't figure out is the logic behind the expression when it
      is wrapped in brackets & a $ sign is appended at the end! Can someone
      please explain me this? What more work does the edited
      ValidationExpre ssion do to make 216 evaluate to True?
      >
      I am aware that $ means the end of a string.
      >
      Thanks,
      >
      Ron
      Check out the Expresso application. Written in dot.net and has a GUI for
      developing regex. Lots of examples of expressions built in and it will
      generate code in both C# and VB.Net. Been using it since dot.net v.0.

      LS

      Comment

      • RN1

        #4
        Re: RegExp Again!

        On Mar 28, 7:46 pm, "Lloyd Sheen" <a...@b.cwrot e:
        "RN1" <r...@rediffmai l.comwrote in message
        >
        news:e4dbd317-a37d-4632-8786-d05581b0199b@n5 8g2000hsf.googl egroups.com...
        >
        >
        >
        >
        >
        Using Regular Expression, I want to ensure that users enter either a 2-
        digit or a 3-digit whole number in a TextBox. This is how I framed the
        ValidationExpre ssion in the RegularExpressi onValidator:
        >
        ---------------------------------------------------------------------------­-----
        <asp:TextBox ID="txt1" runat="server"/>
        <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
        Display="dynami c" ErrorMessage="I nvalid Text"
        ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
        ---------------------------------------------------------------------------­-----
        >
        Suppose a user enters 16 & 216 in the TextBox. As expected, both the
        numbers return True but if I just reverse the ValidationExpre ssion
        i.e. change the ValidationExpre ssion from
        >
        [0-9]{3}|[0-9]{2}
        >
        to
        >
        [0-9]{2}|[0-9]{3}
        >
        & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
        but 216 strangely evaluates to False!
        >
        After a plethora of trial & error methods, I realized that if the
        second ValidationExpre ssion (the one which evaluates 216 to False) is
        enclosed in brackets & a $ sign is appended at the end of the
        expression like this
        >
        ([0-9]{2}|[0-9]{3})$
        >
        then 216 correctly evaluates to True (& so does 16).
        >
        What I couldn't figure out is the logic behind the expression when it
        is wrapped in brackets & a $ sign is appended at the end! Can someone
        please explain me this? What more work does the edited
        ValidationExpre ssion do to make 216 evaluate to True?
        >
        I am aware that $ means the end of a string.
        >
        Thanks,
        >
        Ron
        >
        Check out the Expresso application.  Written in dot.net and has a GUI for
        developing regex.  Lots of examples of expressions built in and it will
        generate code in both C# and VB.Net.  Been using it since dot.net v.0.
        >
        LS- Hide quoted text -
        >
        - Show quoted text -
        Sorry to say, friends, but I don't want an alternate/easier expression
        or examples. Rather I would like to UNDERSTAND the LOGIC behind why

        [0-9]{2}|[0-9]{3}

        evaluates 216 to False & why

        ([0-9]{2}|[0-9]{3})$

        evaluates 216 to True?

        Thanks,

        Ron

        Comment

        • Jesse Houwing

          #5
          Re: RegExp Again!

          Hello RN1,
          Using Regular Expression, I want to ensure that users enter either a
          2- digit or a 3-digit whole number in a TextBox. This is how I framed
          the ValidationExpre ssion in the RegularExpressi onValidator:
          >
          ----------------------------------------------------------------------
          ----------
          <asp:TextBox ID="txt1" runat="server"/>
          <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
          Display="dynami c" ErrorMessage="I nvalid Text"
          ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
          ----------------------------------------------------------------------
          ----------
          Suppose a user enters 16 & 216 in the TextBox. As expected, both the
          numbers return True but if I just reverse the ValidationExpre ssion
          i.e. change the ValidationExpre ssion from
          >
          [0-9]{3}|[0-9]{2}
          >
          to
          >
          [0-9]{2}|[0-9]{3}
          >
          & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
          but 216 strangely evaluates to False!
          >
          After a plethora of trial & error methods, I realized that if the
          second ValidationExpre ssion (the one which evaluates 216 to False) is
          enclosed in brackets & a $ sign is appended at the end of the
          expression like this
          >
          ([0-9]{2}|[0-9]{3})$
          >
          then 216 correctly evaluates to True (& so does 16).
          >
          What I couldn't figure out is the logic behind the expression when it
          is wrapped in brackets & a $ sign is appended at the end! Can someone
          please explain me this? What more work does the edited
          ValidationExpre ssion do to make 216 evaluate to True?
          >
          I am aware that $ means the end of a string.

          To start with, [0-9]{2,3} would be the sorter variant of your original expression,
          but to explain why this isn't working as expected: The RegularExpressi onValidator
          puts a ^ and a $ around every expression, so the expression under test is
          actually:

          ^[0-9]{2}|[0-9]{3}$

          Which should be read as:

          ^[0-9]{2} or [0-9]{3}$

          So: any string starting with 2 numbers or any string ending in two numbers.

          ([0-9]{2}|[0-9]{3})

          Adding () solves this because that in turn expands to

          ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$

          The best solution is to make sure all your validation expressions are either
          in (..), or ^..$.

          --
          Jesse Houwing
          jesse.houwing at sogeti.nl


          Comment

          • RN1

            #6
            Re: RegExp Again!

            On Mar 31, 11:53 pm, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
            wrote:
            Hello RN1,
            >
            >
            >
            >
            >
            Using Regular Expression, I want to ensure that users enter either a
            2- digit or a 3-digit whole number in a TextBox. This is how I framed
            the ValidationExpre ssion in the RegularExpressi onValidator:
            >
            ----------------------------------------------------------------------
            ----------
            <asp:TextBox ID="txt1" runat="server"/>
            <asp:RegularExp ressionValidato r ID="regexp1" ControlToValida te="txt1"
            Display="dynami c" ErrorMessage="I nvalid Text"
            ValidationExpre ssion="[0-9]{3}|[0-9]{2}" runat="server"/>
            ----------------------------------------------------------------------
            ----------
            Suppose a user enters 16 & 216 in the TextBox. As expected, both the
            numbers return True but if I just reverse the ValidationExpre ssion
            i.e. change the ValidationExpre ssion from
            >
            [0-9]{3}|[0-9]{2}
            >
            to
            >
            [0-9]{2}|[0-9]{3}
            >
            & then input 16 & 216 in the TextBox, 16 correctly evaluates to True
            but 216 strangely evaluates to False!
            >
            After a plethora of trial & error methods, I realized that if the
            second ValidationExpre ssion (the one which evaluates 216 to False) is
            enclosed in brackets & a $ sign is appended at the end of the
            expression like this
            >
            ([0-9]{2}|[0-9]{3})$
            >
            then 216 correctly evaluates to True (& so does 16).
            >
            What I couldn't figure out is the logic behind the expression when it
            is wrapped in brackets & a $ sign is appended at the end! Can someone
            please explain me this? What more work does the edited
            ValidationExpre ssion do to make 216 evaluate to True?
            >
            I am aware that $ means the end of a string.
            >
            To start with, [0-9]{2,3} would be the sorter variant of your original expression,
            but to explain why this isn't working as expected: The RegularExpressi onValidator
            puts a ^ and a $ around every expression, so the expression under test is
            actually:
            >
            ^[0-9]{2}|[0-9]{3}$
            >
            Which should be read as:
            >
            ^[0-9]{2} or [0-9]{3}$
            >
            So: any string starting with 2 numbers or any string ending in two numbers..
            >
            ([0-9]{2}|[0-9]{3})
            >
            Adding () solves this because that in turn expands to
            >
            ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
            >
            The best solution is to make sure all your validation expressions are either
            in (..), or ^..$.
            >
            --
            Jesse Houwing
            jesse.houwing at sogeti.nl- Hide quoted text -
            >
            - Show quoted text -
            >So: any string starting with 2 numbers or any string ending >in two numbers
            Shouldn't that be "so any string starting with 2 numbers or any string
            ending in *three* numbers"?

            Ron

            Comment

            • Jesse Houwing

              #7
              Re: RegExp Again!

              Hello RN1,
              On Mar 31, 11:53 pm, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
              wrote:
              >
              >Hello RN1,
              >>
              >>Using Regular Expression, I want to ensure that users enter either a
              >>2- digit or a 3-digit whole number in a TextBox. This is how I
              >>framed the ValidationExpre ssion in the RegularExpressi onValidator:
              >>>
              >>--------------------------------------------------------------------
              >>--
              >>----------
              >><asp:TextBo x ID="txt1" runat="server"/>
              >><asp:RegularE xpressionValida tor ID="regexp1"
              >>ControlToVali date="txt1"
              >>Display="dyna mic" ErrorMessage="I nvalid Text"
              >>ValidationExp ression="[0-9]{3}|[0-9]{2}" runat="server"/>
              >>--------------------------------------------------------------------
              >>--
              >>----------
              >>Suppose a user enters 16 & 216 in the TextBox. As expected, both the
              >>numbers return True but if I just reverse the ValidationExpre ssion
              >>i.e. change the ValidationExpre ssion from
              >>[0-9]{3}|[0-9]{2}
              >>>
              >>to
              >>>
              >>[0-9]{2}|[0-9]{3}
              >>>
              >>& then input 16 & 216 in the TextBox, 16 correctly evaluates to True
              >>but 216 strangely evaluates to False!
              >>>
              >>After a plethora of trial & error methods, I realized that if the
              >>second ValidationExpre ssion (the one which evaluates 216 to False)
              >>is enclosed in brackets & a $ sign is appended at the end of the
              >>expression like this
              >>>
              >>([0-9]{2}|[0-9]{3})$
              >>>
              >>then 216 correctly evaluates to True (& so does 16).
              >>>
              >>What I couldn't figure out is the logic behind the expression when
              >>it is wrapped in brackets & a $ sign is appended at the end! Can
              >>someone please explain me this? What more work does the edited
              >>ValidationExp ression do to make 216 evaluate to True?
              >>>
              >>I am aware that $ means the end of a string.
              >>>
              >To start with, [0-9]{2,3} would be the sorter variant of your
              >original expression, but to explain why this isn't working as
              >expected: The RegularExpressi onValidator puts a ^ and a $ around
              >every expression, so the expression under test is actually:
              >>
              >^[0-9]{2}|[0-9]{3}$
              >>
              >Which should be read as:
              >>
              >^[0-9]{2} or [0-9]{3}$
              >>
              >So: any string starting with 2 numbers or any string ending in two
              >numbers.
              >>
              >([0-9]{2}|[0-9]{3})
              >>
              >Adding () solves this because that in turn expands to
              >>
              >^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
              >>
              >The best solution is to make sure all your validation expressions are
              >either in (..), or ^..$.
              >>
              >--
              >Jesse Houwing
              >jesse.houwin g at sogeti.nl- Hide quoted text -
              >- Show quoted text -
              >>
              >>So: any string starting with 2 numbers or any string ending >in
              >>two numbers
              >>>
              Shouldn't that be "so any string starting with 2 numbers or any string
              ending in *three* numbers"?
              Ron,

              Thank you, you're completely right.

              --
              Jesse Houwing
              jesse.houwing at sogeti.nl


              Comment

              • RN1

                #8
                Re: RegExp Again!

                On Apr 1, 12:58 am, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
                wrote:
                Hello RN1,
                >
                >
                >
                >
                >
                On Mar 31, 11:53 pm, Jesse Houwing <jesse.houw...@ newsgroup.nospa m>
                wrote:
                >
                Hello RN1,
                >
                >Using Regular Expression, I want to ensure that users enter either a
                >2- digit or a 3-digit whole number in a TextBox. This is how I
                >framed the ValidationExpre ssion in the RegularExpressi onValidator:
                >
                >--------------------------------------------------------------------
                >--
                >----------
                ><asp:TextBox ID="txt1" runat="server"/>
                ><asp:RegularEx pressionValidat or ID="regexp1"
                >ControlToValid ate="txt1"
                >Display="dynam ic" ErrorMessage="I nvalid Text"
                >ValidationExpr ession="[0-9]{3}|[0-9]{2}" runat="server"/>
                >--------------------------------------------------------------------
                >--
                >----------
                >Suppose a user enters 16 & 216 in the TextBox. As expected, both the
                >numbers return True but if I just reverse the ValidationExpre ssion
                >i.e. change the ValidationExpre ssion from
                >[0-9]{3}|[0-9]{2}
                >
                >to
                >
                >[0-9]{2}|[0-9]{3}
                >
                >& then input 16 & 216 in the TextBox, 16 correctly evaluates to True
                >but 216 strangely evaluates to False!
                >
                >After a plethora of trial & error methods, I realized that if the
                >second ValidationExpre ssion (the one which evaluates 216 to False)
                >is enclosed in brackets & a $ sign is appended at the end of the
                >expression like this
                >
                >([0-9]{2}|[0-9]{3})$
                >
                >then 216 correctly evaluates to True (& so does 16).
                >
                >What I couldn't figure out is the logic behind the expression when
                >it is wrapped in brackets & a $ sign is appended at the end! Can
                >someone please explain me this? What more work does the edited
                >ValidationExpr ession do to make 216 evaluate to True?
                >
                >I am aware that $ means the end of a string.
                >
                To start with, [0-9]{2,3} would be the sorter variant of your
                original expression, but to explain why this isn't working as
                expected: The RegularExpressi onValidator puts a ^ and a $ around
                every expression, so the expression under test is actually:
                >
                ^[0-9]{2}|[0-9]{3}$
                >
                Which should be read as:
                >
                ^[0-9]{2} or [0-9]{3}$
                >
                So: any string starting with 2 numbers or any string ending in two
                numbers.
                >
                ([0-9]{2}|[0-9]{3})
                >
                Adding () solves this because that in turn expands to
                >
                ^([0-9]{2}|[0-9]{3})$ or ^[0-9]{2}$|^[0-9]{3}$
                >
                The best solution is to make sure all your validation expressions are
                either in (..), or ^..$.
                >
                --
                Jesse Houwing
                jesse.houwing at sogeti.nl- Hide quoted text -
                - Show quoted text -
                >
                >So: any string starting with 2 numbers or any string ending >in
                >two numbers
                >
                Shouldn't that be "so any string starting with 2 numbers or any string
                ending in *three* numbers"?
                >
                Ron,
                >
                Thank you, you're completely right.
                >
                --
                Jesse Houwing
                jesse.houwing at sogeti.nl- Hide quoted text -
                >
                - Show quoted text -
                >Which should be read as:
                >^[0-9]{2} or [0-9]{3}$
                >So: any string starting with 2 numbers or any string ending in three numbers
                As you have pointed out, any string starting with two numbers or any
                string ending with three numbers will evaluate to True. Now 16 is a
                string starting with two numbers & hence evaluates to
                True.....fine.. ..but 216 is a string ending with three numbers; so why
                does it evaluate to False?

                This is getting a bit confusing....

                Ron

                Comment

                Working...