How do I use Java Regex to find all repeating character sequences in a string?

后端 未结 5 1095
借酒劲吻你
借酒劲吻你 2021-01-11 20:38

Parsing a random string looking for repeating sequences using Java and Regex.

Consider strings:

aaabbaaacccbb

I\'d like to find a regular expression

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 21:01

    You could disregard overlap.

    // overlapped 1 or more chars
    (?=(\w{1,}).*\1)
    // overlapped 2 or more chars
    (?=(\w{2,}).*\1)
    // overlapped 3 or more chars, etc ..
    (?=(\w{3,}).*\1)
    

    Or, you could consume (non-overlapped) ..

    // 1 or more chars
    (?=(\w{1,}).*\1) \1
    // 2 or more chars
    (?=(\w{2,}).*\1) \1
    // 3 or more chars, etc ..
    (?=(\w{3,}).*\1) \1
    

提交回复
热议问题