REGEX : move first char to the end of the previous line

  • Thread starter Thread starter teo
  • Start date Start date
T

teo

Hallo

I need to move the first char of a new line
to the last position of the previous line.
The char to move is always the squared bracket: ]




--- Example, I have: ---

Paul and Mary [chapter one
]
The boys went to school.

--- I need to have: ---

Paul and Mary [chapter one]
The boys went to school.




I presume I need to concatenate the ending char of the previuos line
to the first char of the new line, remove the old vbCrLf between them,
then set the new vbCrLf after the squared bracket.

I'm trying to accomplish this by RegEx,
but I have problems.
I'm able to match by using ^]
but
I'm not able to replace.

Below is the code,
what I have to insert instead of ######## ?

---- ---- ----


Dim MyRegex As New Regex("^]", RegexOptions.IgnoreCase Or
RegexOptions.Multiline)

Dim myText As String = ""

myText = "Paul and Mary [chapter one
]
The boys went to school."

Dim myMatches As MatchCollection = myRegex.Matches(myText)




For Each myMatch As Match In myMatches

Debug.Print (myText)

myText = Replace(myText, myMatch.Value, "########")

Debug.Print (myText)

Next
 
Teo.

Is that not a little bit using much time to do a simple operation as (typed
in this message, see it not as a working sample)

\\\
dim mylines() as string = 'whatever
for i as integer = 1 to mylines.length-1
if mylines(i).substring(0,1) = "]" then 'this can shorter by using the char
mylines(i-1) = mylines(i-1) & "]"
end if
next
//,

Something like this is probably about 50 times faster.

Cor
 
You might consider using the String.Replace method. It would be a one-
liner.

someString = someString.Replace(vbCrLf + "]" + vbCrLf, "]" + vbCrLf)

==================
Clay Burch
Syncfusion, Inc.
 
Clay,

You are right, I was thinking if there was no bracked, but than there is
nothing to replace.
(there is in my idea one vbCrLf to much in the first question but that is a
detail).

However I am now afraid that it is a scolar question.

Cor
 
Back
Top