来自 Name 的 REGEX 特定单词
REGEX specific word from Name
我正在尝试找出格式错误的名称的正则表达式,例如用户输入的名称和关系作为一个值
[Son of Joseph Joestar ] => s/o. Joseph Joestar
问题是由于没有验证,用户输入了不同的变体,例如
s/o , s/, s/Joseph... 等
这是我到目前为止得到的
^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
- relation是开头还是开头,后面是名字
- 还有3例女儿(D/o.),妻子(W/o.),父亲(F/o.)
想知道对应的REGEX来过滤关系前缀
提前致谢
也许从这样的事情开始
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} ";
});
我正在尝试找出格式错误的名称的正则表达式,例如用户输入的名称和关系作为一个值
[Son of Joseph Joestar ] => s/o. Joseph Joestar
问题是由于没有验证,用户输入了不同的变体,例如
s/o , s/, s/Joseph... 等
这是我到目前为止得到的
^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
- relation是开头还是开头,后面是名字
- 还有3例女儿(D/o.),妻子(W/o.),父亲(F/o.)
想知道对应的REGEX来过滤关系前缀
提前致谢
也许从这样的事情开始
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} ";
});