[thelist] Re: Re: Re: Regular expression for three or more consecutive occu rrences of the same character

Ben Gustafson Ben_Gustafson at lionbridge.com
Fri Feb 8 16:22:00 CST 2002


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--
[ Picked text/plain from multipart/alternative ]

>> I'm wondering if there's a regular expression... that could be
>> changed to check for n number of the same character in a row.

Howard Cheng wrote:

> The pattern you're looking for would be:
>
> /(\S)\1\1/
>
> In other words, first match a non-whitespace character and look for it
> again two times in a row. To match this n times, you would
> have to keep repeating the "\1" part.
>
> /(\S){3}/ doesn't work because this would match three non-whitespace
> characters in a row, not necessarily three identical characters in a row.

Chris Spruck wrote:

> There are many different approaches to regex and these are a little
> different than what Howard contributed. Some basic regexs would be
> something like these:
>
> q{3} - matches "q" exactly 3 times
> q{3,} - matches "q" at least 3 times
> q{3,5} - matches "q" at least 3 times and no more than 5 times
>
> Those would find just q with those parameters, of course,
> knowing what q would be ahead of time. Where you need to be careful
> would be a case where you have something like :
>
> [a-z]{3}
>
> This won't find 3 q's, but ANY 3 characters, since the match
> that you're looking for 3 times in that expression is any character
> from a to z and not specific to q.

Thanks for the answer to my question and the regex primer, guys. I'll be
using this server-side in ASP, and the scripting engine supports
backreferences.

--Ben



More information about the thelist mailing list