正则表达式从字符串中提取名称到列表中 [英] Regex to extract names from a string into a list

查看:27
本文介绍了正则表达式从字符串中提取名称到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式从以下字符串中提取名称:

How is it possible to use regex just to extract the names from the following string:

Liam got 6,andy got 6

并将其添加到列表中,我试过使用正则表达式,但我找不到正确的表达式来提取名称,并且在这方面仍然有点不稳定.

And add it to a list, i've tried using regex but i cant find the correct expression to extract just the names and am still a bit shaky on this area.

任何帮助将不胜感激

推荐答案

对于简单的情况,我总是建议不要使用 Regex,你可以像这样使用 string.Splitstring.ReplaceLINQ Where:

For simple case, I always recommend not to use Regex, you could do it like this using string.Split, string.Replace, and LINQ Where:

Dim names As String() = sentence.Replace("got ", "").Split(" ").Where(Function(t) Char.IsLetter(t(0))).ToArray()

假设你有这个sentence:

Dim separators As Char() = {",", " "}
Dim names As String() = sentence.Replace("got ", "").Split(separators, System.StringSplitOptions.RemoveEmptyEntries).Where(Function(t) Char.IsLetter(t(0))).ToArray()

逐步发生的是:

"Andy got 6,may got 10, blue got 9, hERald got 0" 
"Andy 6,may 10, blue 9, hERald 0" 'After replace
"Andy" "6" "may" "10" "blue" "9" "hERald" "0" 'After split
"Andy" "may" "blue" "hERald" 'After where

这篇关于正则表达式从字符串中提取名称到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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