开发者

Regular expression to find unbroken text and insert space

I am building a forum and it is in beta right now. The users have begun to exploit certain things, like posting long strings of text with no spaces that will stretch the screen and ruin some styling. I just started using this code and it works fine.

        int charIndex = 0;
        int noSpaceCount = 0;
        foreach (char c in text.ToCharArray())
        {
            if (c != ' ')
                noSpaceCount++;
      开发者_如何学Go      else
                noSpaceCount = 0;

            if (noSpaceCount > 150)
            {
                text = text.Insert(charIndex, " ");
                noSpaceCount = 0;
            }
            charIndex++;
        }

This code works but I'd prefer a regular expression if possible. The problem is that I will be using regular expressions to identify links and I don't want to break long links with a space as those will be fixed by making the link display text abbreviated. So I don't want to insert a space into a piece of text that identifies as a URL, but I do want to insert a space every 150 characters of unbroken, non-link, text.

Any suggestions?


This was surprisingly complicated. Thank Eric and colleagues for the great .NET regex library.

resultString = Regex.Replace(subjectString, 
    @"(?<=     # Assert that the current position follows...
     \s        # a whitespace character
     |         # or
     ^         # the start of the string
     |         # or
     \G        # the end of the previous match.
    )          # End of lookbehind assertion
    (?!(?:ht|f)tps?://|www\.)  # Assert that we're not at the start of a URL.
    (\S{150})  # Match 150 characters, capture them.", 
    "$1 ", RegexOptions.IgnorePatternWhitespace);


replace "([^ ]{150})" with "\1 " globally (modify according to your regex flavor)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜