如何在VB.NET中使用RegEx消除名称前缀和后缀? [英] How do I eliminate name prefixes and suffixes with RegEx in VB.NET?

查看:208
本文介绍了如何在VB.NET中使用RegEx消除名称前缀和后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于名字Mr. John P. Doe Jr.和John P. Doe Jr先生一样,RegEx替换模式可以用来从名称的前面删除先生或先生,以及可以使用什么替换模式从名称的末尾删除Jr.或Jr?我目前用于删除名称前缀的代码如下所示,但它不起作用:

 '   - -populate数组,带有一堆可能的前缀 
Dim lsArrayPrefix As 字符串()= 字符串(){< span class =code-string> mr mrs miss dr prof}

' - 遍历数组寻找匹配使用正则表达式
对于 intCounter = 0 UBound(lsArrayPrefix)
pattern = ^& lsArrayPrefix(intCounter)& \。?\ s +
Regex.Replace(strName,pattern,< span class =code-string>
下一页

解决方案

尝试类似:

 ^(mr |太太|未命中|博士|教授)[\.\s] * 


Given the names "Mr. John P. Doe Jr." and "Mr John P. Doe Jr", what RegEx replace pattern could be used to remove Mr. or Mr from the front of the name, and what replace pattern could be used to remove Jr. or Jr from the end of the name? My current code for eliminating name prefixes looks like this, but it's not working:

'--populate array with a bunch of possible prefixes
Dim lsArrayPrefix As String() = New String() {"mr", "mrs", "miss", "dr", "prof"}

'--loop through the array looking for matches using regexp
For intCounter = 0 To UBound(lsArrayPrefix)
    pattern = "^" & lsArrayPrefix(intCounter) & "\.?\s+"
    Regex.Replace(strName, pattern, "")
Next

解决方案

Try something like:

^(mr|mrs|miss|dr|prof)[\.\s]*


这篇关于如何在VB.NET中使用RegEx消除名称前缀和后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆