It is necessary in the file with the extension. Java to change all the "_" to "". I will use replaceAll. But in the case of String s_s = "ssss_ssss_ssss"; should be String ss = "ssss_ssss_ssss";

Found this: ".*_.*"

What can be better than talking to yourself;) We are looking for in the text ". _. "

  • one
    IMHO, it is impossible to do with regulars. - andy.37
  • one
    Then I am ready to listen to constructive suggestions) For example, first walk and walk and collect everything in a heap all the places where "text_text" occurs and save them somewhere. The second call to change all the remaining _, and then return to the place all that was collected in the first call))) Right now the head will burst) - RattenGW
  • It is necessary to collect not "text_text", but in general all string literals. (For example, s="\"quoted\"_text" ). And this is lexical analysis. - andy.37
  • Of course, everything depends on the contents of the file, but if it is known for sure that the contents are like String s_s = "ssss_ssss_ssss"; , then take each line separately and replace it only in the first case, replaceFirst - Pollux
  • And comments? Of course, you can think up an expression like ("[^"\\]*(?:\\.[^\\"]*)*")|_ , check the contents of the 1st group / submasks, and then decide whether to remove the underscore, but then not using replaceAll, but appendReplacement. - Wiktor Stribiżew

1 answer 1

A regular expression if you need to keep an underline inside quotes and delete it otherwise. Counting on the fact that in each line all quotes are closed.

 _(?=([^"]*[^\\])("([^"\\]|\\.)*"[^"]*)*$) 

To write this in the form of a string constant, you need to screen backslashes and quotes:

 "_(?=([^\"]*[^\\\\])(\"([^\"\\\\]|\\\\.)*\"[^\"]*)*$)" 
  • Man you are a genius !!! - RattenGW