Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete blank lines through regular expressions

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to delete blank lines through regular expressions, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Our goal is to remove blank lines from the text, either one line or multiple lines.

So, my actions are as follows:

Here, we match blank lines with

(\ r\ n) *

In this way, multiple blank lines can be matched and replaced with null, and blank lines can be deleted. This scheme is based on the use of\ r and\ n.

In the past, I saw an implementation on the Internet:

\ n [\ s |] *\ r

After matching, replace it with null, and delete blank lines, as shown below:

Both schemes can achieve the same effect.

If you have other different understandings, please comment and make progress together.

I did the following tests, as shown in the following figure:

Only use

\ n\ r

It is completely possible to delete all blank lines, because at the beginning and end of each blank line

\ n\ r

So, after deleting the previous line, the current line still has a\ n\ r, which can also be matched, so we have to do an alternative operation. In this way, you can delete all blank lines.

Well, if it is,

\ r\ nWhat is the effect?

I did another test, and the effect is as follows:

This is not what 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 dealing with the text, we can use the blank lines like this.

\ r\ n (enter to change lines, enter first, and then line breaks)

To replace it. The text is:

C:\ Program Files\ Common Files\ Relive.dll\ r\ r\ nC:\ Program Files\ Common Files\ svchost.exe\ 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\ 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\ n\ r\ n

This paragraph of text is a whole, no carriage return, no gaps and gaps, this is the text we are dealing with, but carriage return feeds are invisible characters, so they cannot be seen, but they still exist.

Therefore, the situation above us will appear in the course of dealing with it.

With this, we can understand why

After matching and replacing with\ r\ n

All\ r\ nis gone, so the text becomes a connected character-only text.

But

\ n\ r

After replacement

All the\ n\ r in the middle have been replaced,\ r (\ n\ r. \ n\ r)\ n, the middle part has been replaced. So that leaves the last\ r\ n.

Thank you for reading this article carefully. I hope the article "how to remove blank lines through regular expressions" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report