首页 > 解决方案 > 来自名称的 REGEX 特定单词

问题描述

我正在尝试找出格式错误的名称的正则表达式,其中用户输入了名称并将关系作为一个值,例如

[Son of Joseph Joestar ] => s/o. Joseph Joestar

问题是由于没有验证,用户输入了不同的变体,例如

s/o、s/、s/约瑟夫...等

这是到目前为止我得到的

^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
  1. 关系在开头或开头,然后是名称
  2. 还有3个案例女儿(D/o.),妻子(W/o.),父亲(F/o.)

我想知道对应的正则表达式来过滤掉关系前缀

先感谢您

标签: c#regexvalidationtableau-apiregexp-replace

解决方案


也许从这样的事情开始

string foo = "s/o. Joseph Joestar";

// Look (and capture) for SDWF followed by "/" and 
// EITHER "o" and maybe "." and maybe a white space
// OR we look ahead (?=  ) and see a Upper wordchar followeed by lower word chars.
//   look ahead because we do not want to have anything to do with this when replacing
string bar = Regex.Replace(foo, @"^([sSdDwWfF])/(o\.?\s?|(?=[A-Z]\w+))", match =>
{
    string relation = match.Groups[1].Value.ToUpper() switch
    {
        "S" => "Son of",
        "D" => "Daughter of",
        "F" => "Father of",
        "W" => "Wife of",
        _ => throw new Exception("Oops")
    };
    return $"{relation} ";
});


推荐阅读