Search code examples
javaregexregex-group

Replace string in file by regex capture/match group


I want to replace number in line using match/capture group:

define( 'DB_NAME', 'ab20002' );

it's wp-config.php.

I have regex ^define.+DB_NAME.+ab([0-9]{5}).* that works on https://regex101.com/ but in my app, it doesn't work. There is my code:

package test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String file = "/tmp/wp-config.php";
        Path path = Paths.get(file);
        Charset charset = StandardCharsets.UTF_8;

        try {
            String content = new String(Files.readAllBytes(path), charset);
            Pattern pDbName = Pattern.compile("^define.+DB_NAME.+ab([0-9]{5}).*");
            Matcher mDbName = pDbName.matcher(content);
            if (mDbName.find()) {
                System.err.println("mDbName FOUND");
                content = mDbName.replaceAll("XXXXX$1");
            } else {
                System.err.println("mDbName not found");
            }

            Files.write(path, content.getBytes(charset));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

The output should be:

define( 'DB_NAME', 'abXXXXX' );

App can't match, I see "mDbName not found" on stderr.

What I'm doing bad?


Solution

  • I got it! Thanks @anubhava

    I have to use Pattern.MULTILINE:

    Pattern pDbName = Pattern.compile("^define.+DB_NAME.+ab([0-9]{5}).*", Pattern.MULTILINE);
    

    From the documentation:

    In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.

    Complete code:

    package test;
    
    import java.nio.charset.Charset;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
        public static void main(String[] args) {
            String file = "/tmp/wp-config.php";
            Path path = Paths.get(file);
            Charset charset = StandardCharsets.UTF_8;
    
            try {
                String content = new String(Files.readAllBytes(path), charset);
                Pattern pDbName = Pattern.compile("^define.+DB_NAME.+ab([0-9]{5}).*", Pattern.MULTILINE);
                Matcher mDbName = pDbName.matcher(content);
                if (mDbName.find()) {
                    System.err.println("mDbName FOUND");
                    content = mDbName.replaceAll("XXXXX$1");
                } else {
                    System.err.println("mDbName not found");
                }
    
                Files.write(path, content.getBytes(charset));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }