c# - Regex not matching when input string contains an ampersand -


i trying come regex starts letter followed letters, spaces, commas, dots, ampersands, apostrophes , hyphens.

however, ampersand character giving me headaches. whenever appears in input string, regex no longer matches.

i using regex in asp.net project using c# in 'format' property of textinput (a custom control created in project). in it, using regex.ismatch(text, format) match it.

for example, using regex:

^[a-za-z][a-za-z&.,'\- ]*$ 

the results are:

john' william-david                      pass john, william'david     allen--tony-''   pass john, william&david                      fail 

whenever put & in input string regex no longer matches, without works fine.

how can fix issue? why ampersand causing problem?

notes:

  • i've tried escape ampersand ^[a-za-z][a-za-z\&.,'\- ]*$ has same issue
  • i've tried put ampersand @ beginning or end o ^[a-za-z][&a-za-z.,'\- ]*$ or ^[a-za-z][a-za-z.,'\-\& ]*$ doesn't work

your problem somewhere else. following expression evaluates true:

regex.ismatch(@"john, william&david", @"^[a-za-z][a-za-z&.,'\- ]*$") 

see https://dotnetfiddle.net/wdvqnp


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -