1

Want to use Java String.replaceAll(regex, "") to remove all leading spaces of each line in a multi-line text string. if possible remove all carriage-returns as well. What the "regex" should be?

0

1 Answer 1

4

Converting my comment to answer so that solution is easy to find for future visitors.

You may use regex replacement in Java:

str = str.replaceAll("(?m)^\\s+|\\s+$", "");

RegEx Details:

  • (?m): Enable MULTILINE mode so that ^ and $ are matched in every line.
  • ^\\s+: Match 1+ whitespaces at line start
  • |: OR
  • \\s+$: Match 1+ whitespaces before line end
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.