开发者

Regexp matching a string pattern the can contain all or part of a name in C#

I have a requirement to replace all occurence of a lastname. However, the target strings contain ALL the letters of the lastname OR just a FEW characters of it. The target strings is never more than 30 characters long and is subdivided into 'sections' by character "/" (if this information will help)

For example, let's assume we have a last name = FLAHERTY that I am changing to PERRINS

I want to replace all strings for the name above in each string below, character for character. below is a before and after of the example.

MCD开发者_Python百科ONN(3)/FLAH(3)CLAUSSEN expected -> MCDONN(3)/PERR(3)CLAUSSEN

MCDONN(3)/FLAHER(3)/CLAUS expected -> MCDONN(3)/PERRIN(3)/CLAUS

AB/BARAN/SHOOK(ATL)/FLAHERT(SM expected -> AB/BARAN/SHOOK(ATL)/PERRINS(SM

STELL/RUBIN/(STELL/CLAUS/FLAH) expected -> STELL/RUBIN/(STELL/CLAUS/PERR)


That would be one JavaScript-based solution:

var str  = "MCDONN(3)/FLAH(3)CLAUSSEN";
var find = /\bFLA(H(E(R(T(Y?)?)?)?)?)?\b/g; // assuming first 3 chars are req'd
var repl = "PERRINS";

var str2 = str.replace(find, function(match) {
  return repl.substr(0, match.length);
});
// -> "MCDONN(3)/PERR(3)CLAUSSEN"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜