簡體   English   中英

正則表達式將換行符插入特定位置的大文本塊

[英]Regex to insert newlines into large block of text at specific locations

我有一個相當大的文本文件,其中有一堆缺少換行符,這意味着它很亂。 我需要把它分成適當的幾行。

文本現在看起來像這樣:

12345 This is a chunk 23456 This is another chunk 34567 This is yet another chunk 45678 This is yet more chunk 56789 Yet another piece of text

我需要一個正則表達式,它將在每組五位數字之前插入一個換行符(CR/LF 對),結果如下:

12345 This is a chunk 
23456 This is another chunk 
34567 This is yet another chunk 
45678 This is yet more chunk 
56789 Yet another piece of text

可以在第一組數字前加一個,也可以不加; 我可以處理的。

有任何想法嗎? 謝謝。

非常簡單(但不是盡可能“華麗”,因為我懶得使用前瞻):

s/(\d{5})/\r\n\1/gs
s/(?<=\D)(\d{5})(?=\D|$)/\n\1/g

關於“\n”與“\r\n”

這可能取決於手頭的編程語言,但 Perl 和 Python 在 Windows 上將 \ \n替換為\r\n因此在這種情況下,在上述正則表達式中將 \ \n替換為\r\n是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM