java - Enclose group of line having 4 spaces in the beginning -


i want enclose lines in <pre> tag have 4 spaces in beginning.

what have tried?

^[ ]{4}(.*)(?!=^[ ]{4}) 

demo

input:

here code:     string name = "jon";     system.out.println("hello "+name); output:     hello jon 

actual output:

here code:      <pre>string name = "jon";</pre>      <pre>system.out.println("hello "+name);</pre> output:      <pre>hello jon</pre>  

expected output:

here code: <pre>     string name = "jon";     system.out.println("hello "+name); </pre> output: <pre>     hello jon </pre> 

java sample code:

text.replaceall(regex, "<pre>$1</pre>"); 

you can use:

string out = input.replaceall("(?m)((?:^ {4}\\s.*$\\r?\\n)*^ {4}\\s.*$)",                                "<pre>\\n$1\\n</pre>"); 

regex demo

explanation:

(?m)                    # enable multilie mode ^ {4}\\s.*$             # match line exact 4 spaces @ start \\r?\\n                 # followed line feed character (?:^ {4}\\s.*$\\r?\\n)* # match 0 or more of such lines ^ {4}\\s.*$             # followed line exact 4 spaces @ start <pre>\\n$1\\n</pre>     # replace <pre> newline matched block newline </pre> 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -