我有一个输入列表,它以上述格式输入并将它们放入逗号分隔的字符串中。我想在冒号(:)之前和之后获取字符串。
我试过这个正则表达式
string[] reg = Regex.Split(x, @"^(?:[\w ]\:\s[\w]+)+$");
但它似乎无法奏效。请帮忙。
以下是我的代码。这是一个C#控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<string> input = new List<string>();
Console.WriteLine("Please enter your input");
string readinput = Console.ReadLine();
input.Add(readinput);
while (readinput != "")
{
readinput = Console.ReadLine();
input.Add(readinput);
}
string x = string.Join(",", input.ToArray());
Console.WriteLine(x);
// using regex
string[] reg = Regex.Split(x, @"^(?:[\w ]\:\s[\w]+)+$");
Console.WriteLine(reg);
Console.ReadLine();
}
}
}
对不起,我不是很清楚,但是 输入:Amby:Dexter, 德克斯特:卡拉, 卡拉:马特..... 预期产出是Amby,Dexter,Karla,matt ....
答案 0 :(得分:0)
如果我理解正确...用户输入一些字符串,然后用逗号加入它们。之后你想用冒号分割那个字符串?
为什么不使用这样简单的解决方案:
string[] reg = x.Split(':').Select(s => s.Trim()).ToArray();
答案 1 :(得分:0)
也许这会让你开始:
data.list <- gly$x$data
m <- lapply(data.list,function(x){fix_bar_ly(x,"TitleY")})
gly$x$data <- m
gly
或此正则表达式
new Regex(@"(([a-zA-Z])+(?:[\s\:\,]+))").Matches("...");
迭代MatchCollection。