Home > Web Front-end > JS Tutorial > body text

How to remove blank lines from content using regular expressions

php中世界最好的语言
Release: 2018-01-08 10:03:42
Original
2563 people have browsed it

This time I will show you how to delete the blank lines in the content through regular expressions. How to delete the blank lines in the content through regular expressions? What are the precautions on how to delete blank lines in the content through regular expressions? The following is a practical case, let’s take a look.

Here, we use

(\r\n)*
to match blank lines. In this way, we can match multiple blank lines and replace them with
null , you can deleteempty lines. This scheme is based on the use of \r and \n. I have seen an implementation on the Internet before:
\n[\s| ]*\r
After matching, replace it with null. You can also delete empty lines.

Two Both options can achieve the same effect.

Only use

\n\r
to completely delete all blank lines, because the beginning and end of each blank line are
\n\r
So, after deleting the previous line, there is still a \n\r in the current line, which can also be matched, so the replacement operation needs to be done. In this way, all blank lines can be deleted.
Then, if it is
\r\n, what is the effect?

This is not the result we want. why?

I think we can understand it this way,
C:\Program
Files\Common Files\Relive.dll

C:\Program Files\Common Files\svchost. exe

C:\Program Files\Internet Explorer\msvcrt.bak

C:\Program Files\Internet Explorer\msvcrt.dll

C:\Program Files\Internet Explorer\msvcrt .ebk

C:\WINNT\system32\drivers\npf.sys

C:\WINNT\system32\wpcap.dll

C:\WINNT\system32\Packet.dll
C:\WINNT\system32\WanPacket.dll

C:\

Documents and Settings\User name\Local Settings\Temp\wmso.exe C:\Documents and Settings\User name\Local Settings\Temp\BCG5.tmp

This is the original text. In fact, when we are doing text processing, we can use
\r\n(carriage return and line feed, carriage return first, then line feed)
to replace it. The text is:
C:\Program Files\Common Files\Relive.dll\r\n\r\nC:\Program Files\Common Files\svchost.exe\r\n\r\nC:\Program Files\ Internet Explorer\msvcrt.bak\r\nC:\Program Files\Internet Explorer\msvcrt.dll\r\n\nC:\Program Files\Internet Explorer\msvcrt.ebk\r\n\r\n\r\n \r\nC:\WINNT\system32\drivers\npf.sys\r\nC:\WINNT\system32\wpcap.dll\r\nC:\WINNT\system32\Packet.dll\r\nC:\WINNT\system32 \WanPacket.dll\r\n\r\nC:\Documents and Settings\User name\Local Settings\Temp\wmso.exe\r\nC:\Documents and Settings\User name\Local Settings\Temp\BCG5.tmp \r\n\r\n\r\n

This text is a whole, there is no carriage return, no gaps and intervals. This is the text we are processing, but the carriage return and line feed are not visible. characters, so they cannot be seen, but they are still there.
Therefore, our above situation will occur during processing.
With this, we can understand why
After matching and replacing with \r\n,
all the \r\n are gone, so the text becomes a connected plain character text .
But after
\n\r
is replaced, all the \n\r in
are replaced, \r(\n\r……\n\r)\n, The middle part has been replaced. So there is only the last one left.

Related reading:


Example introduction to regular expression form validation

How can the search component of mint-ui be used in The keyboard displays the search button

How to implement springmvc to extract data after receiving the array submitted by jquery

The above is the detailed content of How to remove blank lines from content using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!