Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Regex help

Regex help

Scheduled Pinned Locked Moved Visual Basic
regexhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jholt
    wrote on last edited by
    #1

    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 1 Reply Last reply
    0
    • J jholt

      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 Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • I Ian Darling

        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

        J Offline
        J Offline
        jholt
        wrote on last edited by
        #3

        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

        I 1 Reply Last reply
        0
        • J jholt

          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

          I Offline
          I Offline
          Ian Darling
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups