Regular expression question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

XXXXXXXXXXXXXXXX
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 ?
 
You can use backreferencing to do this..... here is an example that
might do what you want...

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

if you prefer named captures, you can do:

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

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

Karl
 
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 said:
You can use backreferencing to do this..... here is an example that
might do what you want...

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


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

XXXXXXXXXXXXXXXX
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 ?
 
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 said:
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 said:
You can use backreferencing to do this..... here is an example that
might do what you want...

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


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

XXXXXXXXXXXXXXXX
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 ?
 
You can use backreferencing to do this..... here is an example that
might do what you want...

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

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

-mdb
 
Thanks for the help.

Karl Seguin said:
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 said:
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 said:
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.

XXXXXXXXXXXXXXXX
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 ?
 
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in
that's what was asked for (greater than 2)

you're right - my bad.

-mdb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top