I am trying to write a regular list for the string.replaceAll command. The source text looks like this:

dialog_from1_who1 dialog_who1_from1 dialog_from2_who2 dialog_who2_from2 dialog_from3_who3 dialog_who3_from3 

It is necessary to parse in all cases who№, provided that from№ is always known. [dialog_ what to write here I do not know]

Closed due to the fact that the question is too common for the participants cheops , user194374, aleksandr barakin , Kromster , Mr. Black Aug 2 '16 at 1:48 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    it is not clear ... need to replace all the same (replace) or sparss? It seems to be different operations ... If you replace, then what to ... if you parse, then what is expected? - Alexey Shimansky

1 answer 1

 String str = "dialog_from1_who1 dialog_who3_from3" + "dialog_who1_from1" + "dialog_from2_who2" + "dialog_who2_from2" + "dialog_from3_who3" + "dialog_who3_from3"; String newString = str.replaceAll("\\w+from3", "mystring").replaceAll("\\w+from3\\w+", "mystring"); 

\\w+from3 - corresponds to dialog_who1_from3

\\w+from3\\w+ - corresponds to dialog_from3_who3

if the word dialog will always be. then you need to write like that

 String newString = str.replaceAll("dialog_\\w+from3", "mystring").replaceAll("dialog_from3\\w+", "mystring"); 
  • illegal escape character error. Swears on slashes. If I double the slash, then the regular schedule does not work. - Nikola Krivosheya
  • @NikolaKrivosheya corrected. it was necessary to screen them off - Senior Pomidor