Egor  Tyurin Egor Tyurin - 16 days ago 5
Java Question

Java regex to put a string that doesn't start with # inside <p> </p> tag

Input:

#This is a header\nSome line\nAnother line


Desired output:

#This is a header

<p>Some Line</p>

<p>Another line</p>


I've tried this:

s = s.replaceAll("(^|\\n)*(?!#)([^\\n]+)(\\n|$)", "$1<p>$2</p>$3");


But it won't work correctly.
So i'd appreciate your help.

Answer
a.replaceAll("(?m)^(#[^\r\n]+)\r?\n([^\r\n]+)\r?\n([^\r\n]+)$", "<p>$1</p><p>$2</p><p>$3</p>")

Regex: (?m)^(#[^\r\n]+)\r?\n([^\r\n]+)\r?\n([^\r\n]+)$

  • (?m) adds the multine flag. This means The ^ and $ characters behave differently.
  • ^ start of any line
  • (#[^\n\r]+) Group 1: literal # 1 or more non-newline and non-carriage-return characters
  • \r?\n optional carriage-return character, newline and non-carriage-return character
  • ([^\n]+) Group 2: 1 or more non-newline and non-carriage-return characters
  • \r?\n optional carriage-return character, newline and non-carriage-return character
  • ([^\n]+) Group 3: 1 or more non-newline and non-carriage-return characters
  • $ end of any line

Replacer: <p>$1</p><p>$2</p><p>$3</p>

$n will insert that group number. So $1 will become "#This is a header".