Assume:

Some kind of text, some text, {in this block, you need to replace ljatinitsu with kirjillits,} some text, some text, some text, some text, some text, like some text , some text, some text, some text, some text, some text, some text, some text, some text, {in this j block, you need to replace ljatinica with a Kirjillian } some text, some text, some text, some text, {in this j block, you need to replace ljatinica with a Kirjillian,} some kind of text, some tjhext, some text, some then tejkst some article, some text, some text, j some kind of text, some kind of text, some kind of text, some kind of text, some kind of text, some kind of text, some kind of text, some kind of then text, some text,

Something like that. Roughly speaking, you need to go to the block bounded by operator brackets (and, there can be any character out of the brackets, including the "space" and "paragraph or new line, I don’t know"). Again, parentheses are conditional. Maybe another tag. And replace the required characters according to the condition.

Those. - you need to pull out all the blocks between the brackets (read "tags") to make them replaceAll and insert back. As if it was so!

I already had all my thoughts over. This code would somehow be improved:

String start = s.substring(0, s.indexOf("{")); String subString = s.substring(s.indexOf("{"), s.indexOf("}")); String end = s.substring(s.indexOf("}")); 

I can not use it in the cycle. In short, I need help! Especially consider the options with multithreading.

  • With a multithread I probably overdid it))) - JΓΌrgen von Markoff
  • According to what condition do you need to replace the characters inside the brackets? - iramm
  • @iramm yes, any condition. For example, replace all "j" with "*". In this case, "j", which should not be changed outside the brackets. - JΓΌrgen von Markoff
  • answer: stackoverflow.com/questions/603681/… - JΓΌrgen von Markoff

3 answers 3

UPD : Corrected according to @ArtemKonovalov

 String check = "Какой-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, " + "ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст"; String replaceRegex = "[A-Za-z]"; String tagRegex = "\\{.*?"+replaceRegex+".*?}"; //ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅ΠΌ ΠΌΠ°ΠΏΡƒ с соотвСтствиСм символов. Map<String,String> replaceConst = new HashMap<>(); replaceConst.put("a","А"); replaceConst.put("b","Π‘"); //... replaceConst.put("j","Πͺ"); Matcher tagMatcher = Pattern.compile(tagRegex).matcher(check); while (tagMatcher.find()){ String replaceString = tagMatcher.group(); Matcher replaceMatcher = Pattern.compile(replaceRegex).matcher(replaceString); while (replaceMatcher.find()){ replaceString = replaceString.replace(replaceMatcher.group(),replaceConst.get(replaceMatcher.group())); } check = check.replace(tagMatcher.group(),replaceString); } 

Output:

Some text, some text, {in this block you need to replace the lattice with a Kirillian,} some text, some text, some text, some text, some text, some text , some text, some text, some text, some text, some text, some text, some text, some text, {in this block you need to replace the latino with a Kirillian ,} some text, some text, some text, some text, {in this block, you need to replace the Latin alphabet with a Cyrillic,} some text, some text, some text, some then text, some t Kst, some text, some text, some text, some text, some text, some text, some text, some text, some text, some text, some text

  • Does this solution really take into account replacement boundaries? As far as I understood, the author of the question wanted to make a replacement only in the text between the characters "{" and "}" - Artem Konovalov
  • @ArtemKonovalov thanks for the comment. Corrected. The truth now looks not very nice, but it works. - I. Perevoz

I got a few solutions.

If you need to replace a few characters, in a given block, the following method does it:

 private static String replaceAll(String text, Map<Character, Character> symbols, String leftBorder, String rightBorder) { String patternTemplate = Pattern.quote(leftBorder) + "(.*?)" + Pattern.quote(rightBorder); Pattern pattern = Pattern.compile(patternTemplate); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String replaceText = matcher.group(); for (Entry<Character, Character> entry : symbols.entrySet()) replaceText = replaceText.replace(entry.getKey(), entry.getValue()); text = text.replace(matcher.group(), replaceText); } return text; } 

Called as follows:

 Map<Character, Character> symbols = new HashMap<>(); symbols.put('j', 'Πͺ'); symbols.put('ΠΊ', 'K'); symbols.put('Π·', 'Π—'); String result = replaceAll(text, symbols, "{", "}"); 

If, however, you need to replace only the string, then you can use this method:

 private static String replaceAll(String text, String replaceText, String substitutionText, String leftBorderText, String rightBorderText) { String patternTemplate = "(?<=" + Pattern.quote(leftBorderText) + ")" + "(.*?)[" + replaceText + "](.*?)" + "(?=" + Pattern.quote(rightBorderText) + ")"; String substitutionTemplate = "$1" + substitutionText + "$2"; Pattern pattern = Pattern.compile(patternTemplate); Matcher matcher = pattern.matcher(text); if (matcher.find()) { String result = matcher.replaceAll(substitutionTemplate); return replaceAll(result, replaceText, substitutionText, leftBorderText, rightBorderText); } else return text; } 

Call this:

 String replaceText = "j"; String substitution = "Πͺ"; String result = replaceAll(text, replaceText, substitution, "{", "}"); 

    I think you can use the regular season

     String value = "Какой-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, { Π² этомj Π±Π»ΠΎΠΊΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π»jΠ°Ρ‚ΠΈΠ½ΠΈΡ†Ρƒ Π½Π° ΠΊΠΈΡ€jΠΈΠ»Π»ΠΈΡ†Ρƒ, } ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст, ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст"; String result = value.replaceAll("\{.*?\}","ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ тСкст 2"); 

    Search result for regex Regular season search result

    • No good. I need this answer: it was - {in this block, you need to replace the lattice with a Kirjillian,}, it became - {in this block, you need to replace the Latin alphabet with a Kirillian}. The contents of the blocks can be different! - JΓΌrgen von Markoff
    • @ JΓΌrgenvonMarkoff didn’t understand the question. Toast is it necessary to replace J with A ? - Bohdan Korinnyi
    • @ JΓΌrgenvonMarkoff ok, with the help of this regular schedule you can simply search the blocks, then make a regular schedule which will be checked if the data you need to change (or via Java String.contains() ) and change - Bohdan Korinnyi
    • Similar to the work of tags in html. It is necessary in the text which is inside the brackets to change J to A, let's say. And so on throughout the main text. - JΓΌrgen von Markoff
    • @ JΓΌrgenvonMarkoff regulars are not very, but still <b>.*j.*</b> - you are looking at the html j tag if there is a replaceAll to a - Bohdan Korinnyi