RegEx and ReplaceLineEndings by tabs

What is the syntax for replacing line ending characters with horizontal tabs?

Dim linedata As String = line
Dim regex As New RegEx
Dim ro As New RegExOptions
ro.CaseSensitive = False ' Par defaut
regex.Options = ro
regex.SearchPattern = " +"
Var match As RegExMatch = regex.Search(linedata)
//old API 1: Dim normalized As String = ReplaceLineEndings(regex.Replace(linedata, match, ChrB(9)), ChrB(9))
Dim normalized As String = ReplaceLineEndings(ReplaceLineEndings(regex.Replace(linedata, match, String.ChrByte(9))).ToString, String.ChrByte(9).ToString) 'Modify here !
Dim info() As String = normalized.Split(String.ChrByte(9))


Can’t you just use ReplaceLineEndings with the replacement character Chr(9)? I’m not clear as to why you need the RegEx here.


If you really want to use RegEx for some reason

dim rx as new RegEx
rx.SearchPattern = "(?mi-Us)\n"
rx.ReplacementPattern = "\t"

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
rxOptions.ReplaceAllMatches = true

dim replacedText as string = rx.Replace( sourceText )

If you’re on a Mac I’d recommend picking up Kem’s RegExRX app in the app store.
This is a simple example, but it’s super helpful for putting together more complex search patterns.

Thanks. Windows too, from my web site.

Also, I recommend \R as the pattern as that will match any form of EOL.

But if it were my code, I would use ReplaceLineEndings.

1 Like