RegEx help: Splitting Long Line in 80 characters max.

Locked RegEx help: Splitting Long Line in 80 characters max.

  • Monday, February 19, 2007 6:23 PM
     
     

    Hi,

    Is it possible to use Regex to split a long line into 80 characters max? Obviously, no word should be splitted. If RegEx cannot be used to split, can we use RegEx to identify the position to split at?

    For example if a word is spanning from character positions 78 - 82, I would like to get position 77 as the best place to split the string. Please note that I am working on a string which does not have linebreaks as linebreaks are already removed.

     

    Please let me know the regex that I can use in c# for looping through the string to chop it in the multiple lines.

    Thank you,

    XBPatel

All Replies

  • Monday, February 19, 2007 7:16 PM
     
     

    I just wanted to update that I found the answer by OmegaMan in this forum with subject line:

    "A specialized String.Split but on a location instead of a ...". The problem with that RegEx is that it removes special characters like

    Period (.), Exclamation (!), Question Marks (?), etc.

    I would appreciate if Omegaman or someone else can fix that regEx and reply back with new one.

    Here is that regEx:  "(?<Line>.{1,82})(?:\W)"

    Thank you,

    XBPatel

     

  • Monday, February 19, 2007 9:42 PM
     
     Answered

    The last punctuation was dropped from the input string.

    So I added end of string in the last non-capturing group and it worked fine.

    Please note that I was testing with various string lengths so in sample I used 82 characters in previous post.

     

    Here is the new RegEx: 

    @"(?<Line>.{1,80})(?:\W|$)"

    Thanks