Question: I have string like

```
    try (EchoClient echoClient = EchoClient.create()) {
      EchoRequest request =
          EchoRequest.newBuilder().setName().setPoint().setLoooooongNameWithTest().build();
      EchoResponse response = echoClient.echo(request);
    }

```

I need to remove the first 4 white space. Like:

```
try (EchoClient echoClient = EchoClient.create()) {
  EchoRequest request =
      EchoRequest.newBuilder().setName().setPoint().setLoooooongNameWithTest().build();
  EchoResponse response = echoClient.echo(request);
}

```

My approach is to split string by line-breaker and replace the first 4 white space use regex.

```
String[] codes = doc.split("\\r?\\n");
for (int i = 0; i < codes.length; i++) {
  codes[i] = codes[i].replaceFirst("^( ){4}", "");
}
String result = String.join("\n", codes);

```

I wonder if you have any better solution to solve this. Or any way to replace 4 whitespace without loops each line.

Many thanks.

Answer: 
You can do that in single method call with `MULTILINE` mode as:

```
doc = doc.replaceAll("(?m)^ {4}", ""); 

```

[RegEx Demo](https://regex101.com/r/XwU1NQ/1)

**RegEx Details:**

* `(?m)`: Enable inline `MULTILINE` mode so that `^` can be matched after line breaks or start
* `^ {4}`: Match exactly 4 spaces at the start of a line
You can replace all 4 starting whitespaces on new line like below.

```java
String s = //your text here
s.replaceAll("(?m)^ {4}", "");

```