c# - Regular Expression to replace one letter surrounded with dashes -


i try replace specific letter surrounded 1 or 2 dashes letter

examples: modif-i-ed => modifyed (-i- replaced y)

a-im => eim (a- replaced e)

i tried

regex.replace(word, "-?([a-za-z])-", new_letter) 

but generates example modiyyed first example.

the problem once first - becomes optional, there 2 matches inside modif-i-ed: f- , i-. thus, there 2 replacements.

i suggest matching , capturing letters before -x- pattern , return them in match evaluator, , use -?[a-z]- match , replace:

(\b[a-z](?=-))|-?[a-z]- 

c#:

var myletter = "y"; var str = " modif-i-ed  a-im  y-i-eld"; var res = regex.replace(str, @"(\b[a-z](?=-))|-?[a-z]-",       m => m.groups[1].success ? m.groups[1].value : myletter); console.writeline(res); // => modifyed  yim  yyeld 

see ideone demo


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -