スキップしてメイン コンテンツに移動

投稿

ラベル(detect)が付いた投稿を表示しています

Regex: Detect Repeated String Chunks

Target strings I am trying to detect string which contains repeated string pattern like below. .....ABCDABCDABCD..... "ABCD" is just an example, it can be any fixed length of string chunk. Solution Regex Thinking for a bit and finally reached the solution regex which meets my demand is something like below. (.{4,})\\1{3,} The above regex matches string whose length of the chunk is more than 4 and it should be repeated equal or more than 4 times. In more general regex is below (using pattern formatting). String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1) Java Code Ok you know I am Java lover, I will show you full regex code with test cases. If you found any bugs on the code please feel free to comment. public static final boolean isRepeatedStrIn(String input, int minChunkLen, int times) { return input.matches(String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1)); } package com.dukesoftware.utils.common; import java.util.Array