簡體   English   中英

使用任意數量的正則表達式模式添加回車

[英]Add carriage return using regex pattern of any number

我有這樣的文字:

3    Q    I think I started out, I said when4 you first noticed
the oyster beds, it sounded5 like it didn't really concern you, you did not6 believe that the dredging material or the berm7 building material could reach the oyster beds?8    A    That's correct.9    Q

我需要有一個輸出,該輸出可以找到任何數字序列的第一個(即“ 10”不必是1和0的雙精度匹配),並且看起來像這樣(減去我必須在每行之間放置的空格) :

3    Q    I think I started out, I said when

4 you first noticed the oyster beds, it sounded

5 like it didn't really concern you, you did not

6 believe that the dredging material or the berm

7 building material could reach the oyster beds?

8    A    That's correct.

9    Q

在這里,我們可能只想捕獲(\\d+) ,然后用新行和$1替換它:

在此處輸入圖片說明

正則表達式

如果不需要此表達式,則可以在regex101.com中對其進行修改/更改。

演示版

我們可以嘗試匹配模式:

(?<=.)(\d+)

這表示要匹配並捕獲任意大小的數字,只要它不是文本中的第一個數字。 這樣可以避免在以3開頭的第一行之前添加不必要的換行符。 然后,我們可以用換行符替換該捕獲的數字。 這是一個工作腳本:

Dim regex As Regex = new Regex("(?<=.)(\d+)")
Console.WriteLine(regex.Replace("1 stuff10 more stuff", vbCrLf & "$1"))

輸出:

1 stuff
10 more stuff

確保包含Imports Microsoft.VisualBasic以便能夠在您的代碼中使用vbCrLf

暫無
暫無

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

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