简体   繁体   中英

Add carriage return using regex pattern of any number

I have text that looks like this:

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

I need to have an output that finds the first of any numeric sequence (ie "10" doesn't need to be a double match for 1 and 0) and looks looks like this (minus the spaces I had to put between each line):

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

Here, we might just want to capture the (\\d+) , then replace it with a new line and $1 :

在此处输入图片说明

RegEx

If this expression wasn't desired, it can be modified/changed in regex101.com .

Demo

We can try matching on the pattern:

(?<=.)(\d+)

This says to match and capture a number of any size, provided that it is not the first number in the text. This avoids adding an unwanted newline before the first line beginning with 3 . Then, we can replace with a newline followed by that captured number. Here is a working script:

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

This outputs:

1 stuff
10 more stuff

Be certain to include the Imports Microsoft.VisualBasic to be able to use vbCrLf in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM