.NET Regular Expression Matching

In general: how can I combine a word with regular expression rules for a) the beginning, b) the whole word and c) the end?

In particular: how do I match the expression length> = 1, which has the following rules:

  • He cannot have :! @ #
  • It cannot begin with a space or =
  • It can't end with a space.

I tried:

^[^\s=][^!@#]*[^\s]$

But the correspondence ^[^\s=]passes by the first character in the word. Therefore, it also matches words starting with '!' either '@' or '#' (for example: '#ab' or '@aa'). It also makes the word have at least 2 characters (one starting character that is not space or = -and-one non-spatial character at the end).

I got:

^[^\s=(!@#)]\1*$

, . , 1?

+5
2

Cameron ( , ). , .

( - ), . , . , ( ), , , .

foundMatch = Regex.IsMatch(subjectString, @"
    # Match 'word' meeting multiple logical constraints.
    ^             # Anchor to start of string.
    (?=[^!@#]*$)  # It cannot have any of: ! @ #,      AND
    (?![ =])      # It cannot begin with a space or =, AND
    (?!.*\S$)     # It cannot end with a space,        AND
    .{1,}         # length >= 1 (ok to match special 'word')
    \z            # Anchor to end of string.
    ", 
    RegexOptions.IgnorePatternWhitespace);

"regex-logic" .

+4

. :

^[^\s=!@#](?:[^!@#]*[^\s!@#])?$

, !@#. , , -, . - ^ $.

, , () , .

+3

All Articles