[thelist] regexp question
Sam-I-Am
sam at sam-i-am.com
Thu Apr 5 12:04:35 CDT 2001
the current/recent thread reminded me I wanted to update you on this.
Sam-I-Am wrote:
> I'm trying to match repeated characters. And capture how many times they
> are repeated.
> e.g given string '--------_____' I want to know that there are 8 '-'
> characters, followed by 5 '_' characters.
I did get a solution working:
(this was for a javascript problem btw, so I don't have all the
possibilites I would in perl)
re=/(.)\1*/; /* regexp will match any single character, or runs of the
same character. so given 0010000 I could output 2x0,1,4x0
*/
/* this is the basic loop. I'm just writing out the match, but you can
see that there are a load of properties (such as match length) that I
have access to each time around.
*/
while(re.test(str)) { with(RegExp) {
inputStr = str; str=rightContext;
/*
lastMatch (property of the RegExp object) is just that.
lastMatch.length would tell me the length of the match (# repeating
characters)
inputStr is the string that was matched on (taken as input by the
regexp)
str is redefined as the rightContext - everything after the last
match.
so we can feed str back into the regexp, and as long as there's more
there to match, the loop keeps executing
*/
document.write(lastMatch);
}}
thanks to all who offered suggestions. It all helped
Sam
More information about the thelist
mailing list