简体   繁体   中英

Regular Expression Match for a Title

I need to use C# to write a Regular Expression for a title, here is the requirement:

  1. Title is required (length > 0);
  2. Maximum 256 characters (length <= 256);
  3. No character is forbidden, but whitespace only is illegal (the title ONLY containing whitespaces is illegal);
  4. No leading or trailing whitespaces;

I have already have this:

^.{1,256}$

So how can I meet the rule 3?

EDIT:

  1. Explained rule 3 more clearly;
  2. I added rule 4 from Mario's answer.

Use the (?=pattern)

@"^(?=.*\S).{1,256}$"

The (?=pattern) asserts that the specified pattern exists immediately after this location.

So, the regex matches if and only if after the beginning of the string, it matches the pattern .*\\S and if the whole string matches the pattern ^.{1,256}$

You need to use a zero-width assertion:

@"^(?=.*\S).{1,256}$"

(?=.*\\S) matches any sequence of characters that ends in a non-whitespace character, but does not affect the rest of the match.

I'd skip regular expressions completely, because you can just hardcode string cleanup and validation in two simple steps:

  • Use String.Trim(null) to remove all leading/trailing whitespaces.
  • Compare the length of the remaining string.
  • Uppercase the first character (if you want to).

This works, because a name consisting of whitespaces only would be trimmed to 0 length. Also this avoids using titles such as " Let's go!" .

您可以使用[^ \\ s]匹配除空格以外的任何字符

Though my own answer fits my question, but the credit should still go to the other guys (I either upvoted and chose as the correct answer), because I edited my question after their answer.

=====================

I finally came up a pure regex solution (without any extra steps)

^(\S|\S.{0,254}\S)$

(though I don't understand why the parentheses () are important)

The following test cases pass:

    [TestMethod]
    public void CheckTitleTest()
    {
        // Empty
        Assert.IsFalse(CheckTitle(@""));

        // A whitespace
        Assert.IsFalse(CheckTitle(@" "));

        // Multiple whitespace only
        // http://msdn.microsoft.com/en-us/library/t809ektx.aspx
        Assert.IsFalse(CheckTitle("  \t \n \u1680"));

        // Leading whitespaces
        Assert.IsFalse(CheckTitle("  \tabc"));

        // Trailing whitespaces
        Assert.IsFalse(CheckTitle("abc\t "));

        // Leading and trailing whitespaces
        Assert.IsFalse(CheckTitle("  \tabc\t "));

        // Too long: 257 character
        Assert.IsFalse(CheckTitle(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/*"));

        // A normal title
        Assert.IsTrue(CheckTitle(@"This is a normal title"));
        Assert.IsTrue(CheckTitle(@"This is a normal title."));

        // 256 characters
        Assert.IsTrue(CheckTitle(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));

        // A very simple title
        Assert.IsTrue(CheckTitle(@"A"));
        Assert.IsTrue(CheckTitle(@"!"));
        Assert.IsTrue(CheckTitle(@"\"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM