私は上記の形式で入力を取り、コンマで区切った文字列にする入力リストを持っています。私はコロン(:)の前後に文字列を取得したいと思います。次の文字列の正規表現パターンAmby:Dexter、Dexter:Karla
私はこの正規表現パターン
string[] reg = Regex.Split(x, @"^(?:[\w ]\:\s[\w]+)+$");
を試みたが、動作するように見えるdoesntの。助けてください。
以下は私のコードです。 Amby:デクスター、 デクスター:カーラ、 カーラ:マット..... 予想される出力はAmby、デクスター、あるこれは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();
}
}
}
私は非常に明確ではありませんでした申し訳ありませんが、 入力され、 Karla、matt ....
期待される出力は?現在の正規表現は、あなたが私たちに与えた入力文字列と一致しません。 –
'^(?:[\ w] \:\ s [\ w] +)+ $'はアンカーパターンであり、 'Regex.Split'でそれを使うのは意味がありません。 –
不明な点:完全な文字列でコロンの間の文字列を検索したいのですか(その場合はカンマで区切って表示するのはなぜですか)か、コンマで区切られた文字列のすべてのカンマ区切りでコロンの間の文字列を検索しますか? –