Regular expression for n consecutive characters

K

Kannan S

How do I replace consecutive occurances of any character in a string,
to just one. eg: "aaabbbcc" to "abc"

Pattern repeatedChars = Pattern.compile("([a-z]){2,}+");
String s1 = repeatedChars.matcher("aaabbbcc").replaceAll("\\1");

The regex part seems to be correct, but all consecutive occurances are
replaced by a single "." and s1 is now "...".

Any comments?

Thanks
--kannan
 
S

shakah

Kannan said:
How do I replace consecutive occurances of any character in a string,
to just one. eg: "aaabbbcc" to "abc"

Pattern repeatedChars = Pattern.compile("([a-z]){2,}+");
String s1 = repeatedChars.matcher("aaabbbcc").replaceAll("\\1");

The regex part seems to be correct, but all consecutive occurances are
replaced by a single "." and s1 is now "...".

Any comments?

Thanks
--kannan

'([a-z])\1+' worked for me:

jc@sarah:~/tmp$ cat regextest.java
public class regextest {
public static void main(String [] asArgs) {
java.util.regex.Pattern repeatedChars =
java.util.regex.Pattern.compile(asArgs[0]) ;
for(int nArg=1; nArg<asArgs.length; ++nArg) {
System.out.println("replacement on '" + asArgs[nArg]
+ "' => '" +
repeatedChars.matcher(asArgs[nArg]).replaceAll("$1")
+ "'"
) ;
}
}
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java regextest '([a-z])\1+'
aaaabbcc aabbbbbcddde
replacement on 'aaaabbcc' => 'abc'
replacement on 'aabbbbbcddde' => 'abcde'
 
H

hiwa

Kannan S said:
How do I replace consecutive occurances of any character in a string,
to just one. eg: "aaabbbcc" to "abc"

Pattern repeatedChars = Pattern.compile("([a-z]){2,}+");
String s1 = repeatedChars.matcher("aaabbbcc").replaceAll("\\1");

The regex part seems to be correct, but all consecutive occurances are
replaced by a single "." and s1 is now "...".

Any comments?

Thanks
--kannan
Don't use regex for such simple problem. Use one or two of plain
String
methods.

From java.util.regex.Pattern javadoc:
<quote>
The captured input associated with a group is always the subsequence
that the group most recently matched. If a group is evaluated a second
time because of quantification then its previously-captured value, if
any, will be retained if the second evaluation fails. Matching the
string "aba" against the expression (a(b)?)+, for example, leaves
group two set to "b". All captured input is discarded at the beginning
of each match.
</quote>
 

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

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top