The state of the expression model is false

I created the following regular expression:

(^[%a-zA-Z][\w\-.]+)( [\w\-.]+|[\w\-.]+)([%A-Za-z.]$)

Regular expression visualization

which should be allowed, for example.

test (working)
test test (working)
%test (working)
test% (working)
%test% (working)
% (not working)

I tried the following: make% only works:

(%|(^[%a-zA-Z][\w\-.]+)( [\w\-.]+|[\w\-.]+)([%A-Za-z.]$))

Regular expression visualization

a certain template works fine with https://www.debuggex.com , but it crashes in my application where the model looks:

[Display(Name = "Publisher")]
[RegularExpression(@"(%|(^[%a-zA-Z][\w\-.]+)( [\w\-.]+|[\w\-.]+)([%A-Za-z.]$))", ErrorMessage = "Invalid Expression!")]    
public string SoftwarePublisher { get; set; }

any advice would be helpful. thank you in advance

+4
source share
2 answers

This template should be better than yours, try it as it works for me:

PATTERN

(^|%)((\w+\s?)+)([^\s]$|%)|%

INPUT

case 1:    test
case 2:    test
case 3:    %test
case 4:    test%
case 5:    %test%
case 6:    %

OUTPUT

case 1:    matches
case 2:    matches
case 3:    matches
case 4:    matches
case 5:    matches
case 6:    matches

Hope this works for you, or at least gives you some hint. If you check it out on any website, use all the lines separately when I use ^and $.

+1

, , 4 . 2 1, 1 ( 2) 2 1 3.

, "tes", , "%%%%", ( % ).

, , . , , .

:

, :

  • 3

  • '_'

  • .

0

All Articles