0

I have a list of names for which I am trying to implement proper casing- the list of names are as follows

  • DR. JOHN GROVE WHITE
  • RONALD MCDONALD
  • GORDON SAINT JOHN
  • ARTHUR STEVENS
  • GAIL VAN STOLK
  • ANWAR AL-SADAT
  • SYMS III, ROGER
  • ROGER SYMS V

For the above list I have implemented regex pattern. The regex pattern are working properly when i put then on regex testing websites but it is not working in code

Regex.IsMatch(value, @"(/^(van|von|der|la|d[aeio]|d[ao]s|dit)[\s,]*$/i)")

The above is not matching when i pass value as van in code

Regex.IsMatch(value, @"(/(^|\s)+(Mc|[DO]\'|St\.|St[\.]?[\s]|Dewolf)/i)")

The above is not working when i pass value as Mcdonald

Regex.IsMatch(value, @"(/(^|\s*)(Mac)(allist|arth|b|c(allu|art|ask|l|r|ull)|d|f|g|i(nn|nty|saa|v)|kinn|kn|l(a|ea|eo)|m|na[mu]|n[ei]|ph|q|ra|sw|ta|w)/i)")

The above regex is not matching when i pass value as Macdonald

I am not able to find out is there any issue in regex.Please help me out for the above.Thanks in advance

JGNI
  • 3,933
  • 11
  • 21
  • The above regex patterns is for multiple surname,name,ordianal scenario from which van,Macdonald are some of valid values – shafi ahmed May 06 '20 at 07:15
  • Try it without using the `/i` and without the start anchor `^` for example `Regex.IsMatch("van test", @"(\b(van|von|der|la|d[aeio]|d[ao]s|dit)[\s,]*)");` See this page for case insensitive https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regexoptions?view=netcore-3.1#System_Text_RegularExpressions_RegexOptions_IgnoreCase – The fourth bird May 06 '20 at 07:40
  • thanks it worked for above regex ,please do help for other pattern too – shafi ahmed May 06 '20 at 08:01
  • Did you try the same apprach for the other patterns? – The fourth bird May 06 '20 at 08:25
  • I did these to Regex.IsMatch(value, @"(\b(i{3}|i{1,2}v?|v?i{1,2})[\s,]*)" . These regex pattern is to match ordinal Ordinal suffixes (I - VIII only) in name. for example name like Roger Syms V , but it not worked. others worked, thanks – shafi ahmed May 06 '20 at 08:42
  • That is because you are trying to match at least 1 or 2 times a `i` before the optional `v` Try `\b(i{3}|i{0,2}v|vi{1,2})[\s,]*` If you want to match roman numerals, see https://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression – The fourth bird May 06 '20 at 09:04
  • The above regex for roman literal is also matching with alphabet 'v'. i dont want this to happen . is there any way to avoid the same so that it matched only with roman literals not with any alphabets – shafi ahmed May 06 '20 at 10:07
  • Are you using the `RegexOptions.IgnoreCase` option? See this page how to enable and disable ignore case inline https://stackoverflow.com/questions/3542042/how-to-use-inline-modifiers-in-c-sharp-regex/3542048 – The fourth bird May 06 '20 at 10:20

0 Answers0