Regex replace repeating line pattern

I need to replace the repeating pattern in the word with each basic structural element. For example, I have the string "TATATATA" and I want to replace it with "TA". In addition, I would probably replace more than 2 repetitions to avoid replacing ordinary words.

I am trying to do this in Java using the replaceAll method.

+2
source share
3 answers

I think you need this (works for any length of a repeating string):

String result = source.replaceAll("(.+)\\1+", "$1") 

Or, alternatively, to determine shorter matches:

 String result = source.replaceAll("(.+?)\\1+", "$1") 

It matches the group of letters first, and then again (using the backlink in the matching pattern). I tried this and it seems to have done the trick.


Example

 String source = "HEY HEY duuuuuuude what'' up? Trololololo yeye .0.0.0"; System.out.println(source.replaceAll("(.+?)\\1+", "$1")); // HEY dude what up? Trolo ye .0 
+6
source

You are better off using Pattern here than .replaceAll() . For instance:

 private static final Pattern PATTERN = Pattern.compile("\\b([AZ]{2,}?)\\1+\\b"); //... final Matcher m = PATTERN.matcher(input); ret = m.replaceAll("$1"); 

edit: example:

 public static void main(final String... args) { System.out.println("TATATA GHRGHRGHRGHR" .replaceAll("\\b([A-Za-z]{2,}?)\\1+\\b", "$1")); } 

Fingerprints:

 TA GHR 
+1
source

Since you requested a solution for regex:

 (\\w)(\\w)(\\1\\2){2,}; 

(\w)(\w) : matches each pair of consecutive word characters ( (.)(.) will capture every pair of characters of any type), storing them in capture groups 1 and 2. (\\1\\2) matches in any time that characters in these groups repeat again immediately afterwards, and {2,} matches when it repeats two or more times ( {2,10} will match when it repeats more than one than ten times).

 String s = "hello TATATATA world"; Pattern p = Pattern.compile("(\\w)(\\w)(\\1\\2){2,}"); Matcher m = p.matcher(s); while (m.find()) System.out.println(m.group()); //prints "TATATATA" 
+1
source

Source: https://habr.com/ru/post/1246016/


All Articles