Search code examples
javaregexstringreplaceall

Java RegEx - How to prevent that a row of special characters is replaced by just one other character?


What I want to do with the Regular Expression (Regex) is basically the opposite to this request.

I currently use this pattern for creating URL compatible file names:

String new_file_name = old_file_name.replaceAll("[^a-zA-Z0-9._]+", "_"); 

If have for example example

String old_file_name = "!§$%&{}[]()=´´```_lol_á_^_°.PNG";

then new_file_name will be the following with the above pattern: __lol______.PNG

However, I want to have it differently. This regex pattern removes whole consecutive rows of special characters by just one underscore but I want that the pattern to replace EVERY SINGLE OCCURRENCE of a special character with its own underscore which would give me this result instead:

__________________lol______.PNG 

Is there a way to achieve this with regex?

Post-answer Edit:
Thank you guys for your fantastic answers! Really helped me a lot! For completeness sake I want to provide you my clunky and awkward code snippet with which I achieved the same result like the regular expression you stated:

  for(int i = 0; i < old_file_name.length(); i++)
    {
        // if it is a letter, a number, a underscore or a dot
        if(     (((int)old_file_name.charAt(i) > 47)  &&  ((int)old_file_name.charAt(i) < 58 )) ||
                (((int)old_file_name.charAt(i) > 64)  &&  ((int)old_file_name.charAt(i) < 91 )) ||
                (((int)old_file_name.charAt(i) > 96)  &&  ((int)old_file_name.charAt(i) < 123 )) ||
                ((int)old_file_name.charAt(i) == 95) ||
                ((int)old_file_name.charAt(i) == 46)  )
        {

            // do nothing
        } // end if
        else
        {
            new_file_name = new_file_name.replace(old_file_name.charAt(i), '_');

            // System.out.println("Step " + i + " : " + new_file_name); // for debugging
        } // end if-else
     } // end for 

Solution

  • You have to remove the + in the end [^a-zA-Z0-9._] so you can use :

    String new_file_name = old_file_name.replaceAll("[^a-zA-Z0-9._]", "_");
    //-------------------------------------------------------------^
    

    Output

    __________________lol______.PNG 
    

    because + mean Match one or more times. but in your case you need to replace all them so you don't need the +.