Regular expression question

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

    #1

    Regular expression question

    I am trying to formulate a regular expression that would not allow strings
    such as the following to be entered.

    XXXXXXXXXXXXXXX X
    AAAAAAAAAAAAAAA
    BBBBBBBBB

    Basically any repeating character > 2 of the same character.

    I have tried several things and I can't seem to get it to work.

    Any suggestions ?
  • John Murray

    #2
    Re: Regular expression question

    You can use backreferencing to do this..... here is an example that
    might do what you want...

    ([a-zA-Z])\1{2,}


    David McCollough wrote:[color=blue]
    > I am trying to formulate a regular expression that would not allow strings
    > such as the following to be entered.
    >
    > XXXXXXXXXXXXXXX X
    > AAAAAAAAAAAAAAA
    > BBBBBBBBB
    >
    > Basically any repeating character > 2 of the same character.
    >
    > I have tried several things and I can't seem to get it to work.
    >
    > Any suggestions ?[/color]

    Comment

    • Karl Seguin [MVP]

      #3
      Re: Regular expression question

      (.)\1{2}

      if you prefer named captures, you can do:

      (?<char>.)\k<ch ar>{2}

      in c# you'll need to escape the \ with \\ unless it isn't in codebehind..

      Karl

      --
      MY ASP.Net tutorials
      Programming blog exploring Zig, Elixir, Go, Testing, Design and Performance



      "David McCollough" <david.mccollou gh@newsgroups.n ospam> wrote in message
      news:92F3CF39-B846-44D8-9B49-43908AC8A81C@mi crosoft.com...[color=blue]
      >I am trying to formulate a regular expression that would not allow strings
      > such as the following to be entered.
      >
      > XXXXXXXXXXXXXXX X
      > AAAAAAAAAAAAAAA
      > BBBBBBBBB
      >
      > Basically any repeating character > 2 of the same character.
      >
      > I have tried several things and I can't seem to get it to work.
      >
      > Any suggestions ?[/color]


      Comment

      • David McCollough

        #4
        Re: Regular expression question

        John

        Thanks so much for the help.

        I understand what the [a-zA-Z] does and I understand what the {2,} does.

        What does the \1 do ??

        "John Murray" wrote:
        [color=blue]
        > You can use backreferencing to do this..... here is an example that
        > might do what you want...
        >
        > ([a-zA-Z])\1{2,}
        >
        >
        > David McCollough wrote:[color=green]
        > > I am trying to formulate a regular expression that would not allow strings
        > > such as the following to be entered.
        > >
        > > XXXXXXXXXXXXXXX X
        > > AAAAAAAAAAAAAAA
        > > BBBBBBBBB
        > >
        > > Basically any repeating character > 2 of the same character.
        > >
        > > I have tried several things and I can't seem to get it to work.
        > >
        > > Any suggestions ?[/color]
        >[/color]

        Comment

        • Karl Seguin [MVP]

          #5
          Re: Regular expression question

          it's a back reference \1 references the first group capture. A capture is
          anything between unescaped parenthesis ( )

          Karl
          --
          MY ASP.Net tutorials
          Programming blog exploring Zig, Elixir, Go, Testing, Design and Performance



          "David McCollough" <david.mccollou gh@newsgroups.n ospam> wrote in message
          news:50A814DA-140B-478E-8C64-AA4F89CE3DDA@mi crosoft.com...[color=blue]
          > John
          >
          > Thanks so much for the help.
          >
          > I understand what the [a-zA-Z] does and I understand what the {2,} does.
          >
          > What does the \1 do ??
          >
          > "John Murray" wrote:
          >[color=green]
          >> You can use backreferencing to do this..... here is an example that
          >> might do what you want...
          >>
          >> ([a-zA-Z])\1{2,}
          >>
          >>
          >> David McCollough wrote:[color=darkred]
          >> > I am trying to formulate a regular expression that would not allow
          >> > strings
          >> > such as the following to be entered.
          >> >
          >> > XXXXXXXXXXXXXXX X
          >> > AAAAAAAAAAAAAAA
          >> > BBBBBBBBB
          >> >
          >> > Basically any repeating character > 2 of the same character.
          >> >
          >> > I have tried several things and I can't seem to get it to work.
          >> >
          >> > Any suggestions ?[/color]
          >>[/color][/color]


          Comment

          • Michael Bray

            #6
            Re: Regular expression question

            John Murray <jmurray@pluck. com> wrote in news:#xgjynGHGH A.1028
            @TK2MSFTNGP11.p hx.gbl:
            [color=blue]
            > You can use backreferencing to do this..... here is an example that
            > might do what you want...
            >
            > ([a-zA-Z])\1{2,}
            >
            >[/color]

            Wouldn't that make it so there couldn't be THREE in a row?

            -mdb

            Comment

            • David McCollough

              #7
              Re: Regular expression question

              Thanks for the help.

              "Karl Seguin [MVP]" wrote:
              [color=blue]
              > it's a back reference \1 references the first group capture. A capture is
              > anything between unescaped parenthesis ( )
              >
              > Karl
              > --
              > MY ASP.Net tutorials
              > http://www.openmymind.net/
              >
              >
              > "David McCollough" <david.mccollou gh@newsgroups.n ospam> wrote in message
              > news:50A814DA-140B-478E-8C64-AA4F89CE3DDA@mi crosoft.com...[color=green]
              > > John
              > >
              > > Thanks so much for the help.
              > >
              > > I understand what the [a-zA-Z] does and I understand what the {2,} does.
              > >
              > > What does the \1 do ??
              > >
              > > "John Murray" wrote:
              > >[color=darkred]
              > >> You can use backreferencing to do this..... here is an example that
              > >> might do what you want...
              > >>
              > >> ([a-zA-Z])\1{2,}
              > >>
              > >>
              > >> David McCollough wrote:
              > >> > I am trying to formulate a regular expression that would not allow
              > >> > strings
              > >> > such as the following to be entered.
              > >> >
              > >> > XXXXXXXXXXXXXXX X
              > >> > AAAAAAAAAAAAAAA
              > >> > BBBBBBBBB
              > >> >
              > >> > Basically any repeating character > 2 of the same character.
              > >> >
              > >> > I have tried several things and I can't seem to get it to work.
              > >> >
              > >> > Any suggestions ?
              > >>[/color][/color]
              >
              >
              >[/color]

              Comment

              • Karl Seguin [MVP]

                #8
                Re: Regular expression question

                that's what was asked for (greater than 2)

                karl

                --
                MY ASP.Net tutorials
                Programming blog exploring Zig, Elixir, Go, Testing, Design and Performance



                "Michael Bray" <mbray@makeDInt oDot_ctiusaDcom > wrote in message
                news:Xns9751605 42AB99mbrayctiu sacom@207.46.24 8.16...[color=blue]
                > John Murray <jmurray@pluck. com> wrote in news:#xgjynGHGH A.1028
                > @TK2MSFTNGP11.p hx.gbl:
                >[color=green]
                >> You can use backreferencing to do this..... here is an example that
                >> might do what you want...
                >>
                >> ([a-zA-Z])\1{2,}
                >>
                >>[/color]
                >
                > Wouldn't that make it so there couldn't be THREE in a row?
                >
                > -mdb[/color]


                Comment

                • Michael Bray

                  #9
                  Re: Regular expression question

                  "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
                  net> wrote in news:ORZVqedHGH A.2940@tk2msftn gp13.phx.gbl:
                  [color=blue]
                  > that's what was asked for (greater than 2)
                  >[/color]

                  you're right - my bad.

                  -mdb

                  Comment

                  Working...