Regex help
-
Hi I need to format a long string into user definable line lengths, i.e. The cat sat on the mat and the dog bit the cat (linelength=10) The cat sat on the mat and the dog bit the cat The cat sat on the mat and the dog bit the cat (linelength=11) The cat sat on the mat and the dog bit the cat Can any of the regext gurus help me with this one? Note that a line ending can be a comma, semicolon, color, period. Many thanks Jeremy Holt
-
Hi I need to format a long string into user definable line lengths, i.e. The cat sat on the mat and the dog bit the cat (linelength=10) The cat sat on the mat and the dog bit the cat The cat sat on the mat and the dog bit the cat (linelength=11) The cat sat on the mat and the dog bit the cat Can any of the regext gurus help me with this one? Note that a line ending can be a comma, semicolon, color, period. Many thanks Jeremy Holt
I'm not sure a regexp is actually the easiest way to do this. Why not use Split() with a space delimiter to get an array of each word, and then just concatenate the array together again to the appropriate lengths? -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
I'm not sure a regexp is actually the easiest way to do this. Why not use Split() with a space delimiter to get an array of each word, and then just concatenate the array together again to the appropriate lengths? -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
Ian You're quite right, I don't know whay hadn't thought of that before. I wrote this which seems to work: Public Shared Function ToParagraph(ByVal Text As String, ByVal LineLength As Integer) As String Dim arr As String() = Text.Split(New Char() {" "c}) Dim sb As New Text.StringBuilder Dim CurrentLine As Integer = 0 For i As Integer = 0 To arr.Length - 1 If CurrentLine + arr(i).Length + 1 <= LineLength Then CurrentLine += (arr(i).Length + 1) sb.Append(arr(i) & " ") Else CurrentLine = arr(i).Length + 1 sb.Append(vbCrLf & arr(i) & " ") End If Next Return sb.ToString.TrimEnd End Function Regards Jeremy
-
Ian You're quite right, I don't know whay hadn't thought of that before. I wrote this which seems to work: Public Shared Function ToParagraph(ByVal Text As String, ByVal LineLength As Integer) As String Dim arr As String() = Text.Split(New Char() {" "c}) Dim sb As New Text.StringBuilder Dim CurrentLine As Integer = 0 For i As Integer = 0 To arr.Length - 1 If CurrentLine + arr(i).Length + 1 <= LineLength Then CurrentLine += (arr(i).Length + 1) sb.Append(arr(i) & " ") Else CurrentLine = arr(i).Length + 1 sb.Append(vbCrLf & arr(i) & " ") End If Next Return sb.ToString.TrimEnd End Function Regards Jeremy
jholt wrote: Ian You're quite right, I don't know whay hadn't thought of that before. I wrote this which seems to work: That's ok, no charge :-D Glad to help -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky