java - Replacing multiple occurrences of characters -


i found

string seq = "123456789";     string regex = seq.replaceall(".", "(?=[$0-9]([a-z]))?") + "[0-9][a-z]";     string repl = seq.replaceall(".", "\\$$0"); 

which turns 4a aaaa, 3b bbb , on... need opposite , couldn't figure out. need turn aaaa 4a, bbb 3b , on. lot

here example of run-length encoding/decoding implementation in java:

import java.util.regex.matcher; import java.util.regex.pattern; public class runlengthencoding {      public static string encode(string source) {         stringbuffer dest = new stringbuffer();         (int = 0; < source.length(); i++) {             int runlength = 1;             while (i+1 < source.length() && source.charat(i) == source.charat(i+1)) {                 runlength++;                 i++;             }             dest.append(runlength);             dest.append(source.charat(i));         }         return dest.tostring();     }      public static string decode(string source) {         stringbuffer dest = new stringbuffer();         pattern pattern = pattern.compile("[0-9]+|[a-za-z]");         matcher matcher = pattern.matcher(source);         while (matcher.find()) {             int number = integer.parseint(matcher.group());             matcher.find();             while (number-- != 0) {                 dest.append(matcher.group());             }         }         return dest.tostring();     }      public static void main(string[] args) {         string example = "wwwwwwwwwwwwbwwwwwwwwwwwwbbbwwwwwwwwwwwwwwwwwwwwwwwwbwwwwwwwwwwwwww";         system.out.println(encode(example));         system.out.println(decode("1w1b1w1b1w1b1w1b1w1b1w1b1w1b"));     } } 

taken here:

http://rosettacode.org/wiki/run-length_encoding

(this page includes equivalent examples in 72 different programming languages achieve same goal)

to achieve asking for, use "encode" method.

tested here: http://www.browxy.com/submittedcode/21369

regex on own not suitable tool trying achieve this.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -