c# - 来自名称的 REGEX 特定单词
问题描述
我正在尝试找出格式错误的名称的正则表达式,其中用户输入了名称并将关系作为一个值,例如
[Son of Joseph Joestar ] => s/o. Joseph Joestar
问题是由于没有验证,用户输入了不同的变体,例如
s/o、s/、s/约瑟夫...等
这是到目前为止我得到的
^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
- 关系在开头或开头,然后是名称
- 还有3个案例女儿(D/o.),妻子(W/o.),父亲(F/o.)
我想知道对应的正则表达式来过滤掉关系前缀
先感谢您
解决方案
也许从这样的事情开始
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} ";
});
推荐阅读
- c++ - 构造函数的初始化列表
- javascript - XMLHttpRequest 获取请求响应为空,即使有回调
- angular - PrimeNG turbo 表单个单元格操作与 observable
- xml - SOAP XML 有效 xsi:type
- javascript - 如何使用 Node.js 和 simple-oauth2 对 LinkedIn 进行身份验证
- python - 使用正则表达式查找所有完全大写的短语
- excel - 通过数组公式计算两个其他日期之间日期出现的公式
- python - Python:如何将符号表达式转换为函数以便进行数值积分?
- asp.net - EPPlus ASP.NET Excel 工作表在下载时为空白
- python - 从 RDD Apache Spark 系统保存图像