The regular expression for the name

I need to use C # to write a regex for the header, this is what:

  • Name required (length> 0);
  • Maximum 256 characters (length <= 256);
  • A character is forbidden, but spaces are only illegal (a header containing only spaces is illegal);
  • Leading or trailing spaces are missing;

I already have this:

^.{1,256}$ 

So how can I fulfill rule 3?

EDIT:

  • Rule 3 is explained in more detail;
  • I added rule 4 from Mario's answer.
+4
source share
5 answers

I would completely skip regexes, because you can just hard-clear the string and check it in two simple steps:

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

This works because a name consisting of spaces will only be truncated to 0. It also avoids the use of names such as " Let go!" .

+2
source

Use (?=pattern)

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

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

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

+2
source

You need to use the zero width statement:

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

(?=.*\S) matches any character sequence that ends with a character without spaces, but does not affect the rest of the match.

+2
source

You can use [^ \ s] to match any character except space

0
source

Although my own answer fits my question, but the loan should still go to the other guys (I either supported or chose the right answer) because I edited my question after they answered.

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

Finally I came up with a clean regular solution (without any extra steps)

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

(although I don't understand why 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(@"\")); } / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / *"));  [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(@"\")); } / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /"));  [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(@"\")); } 
0
source

All Articles