我有一组基于我分割字符串的值
string[] seperator = new string[9];
seperator[0] = "*"; //is the client
seperator[1] = "/"; //is the name of company
seperator[2] = "("; //name of the market
seperator[5] = ":"; //ID
seperator[6] = "?"; //orderType
seperator[3] = "!@"; //realtive Time
seperator[4] = "!+"; //
seperator[7] = "+"; //quantity
seperator[8] = "@";//price
string[] result = values.Split(seperator, StringSplitOptions.None);
Run Code Online (Sandbox Code Playgroud)
例如:输入字符串是*A/AB(M!@ 12:6?SIMPLE!+ 5 + 2
OUTPUT
[0]: ""
[1]: "A"
[2]: "AB"
[3]: "M"
[4]: "12"
[5]: "6"
[6]: "SIMPLE"
[7]: "5"
[8]: "2"
例如:输入字符串是*A(M!@ 12?SIMPLE!+ 5 + 2/AB:6
OUTPUT:
[0]: ""
[1]: "A"
[2]: "M"
[3]: "12"
[4]: "SIMPLE"
[5]: "5"
[6]: "2"
[7]: "AB"
[8]: "6"
我面临的问题是:我怎么能说A是客户,AB是公司......等等
作为用户输入此信息的顺序RANDOM ...如果他没有输入这些值中的任何一个,它会改变结果长度吗?
如何使用一个或多个带有命名捕获组的正则表达式并按名称索引匹配?
这是一个让你入门的例子:
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
Regex regex = new Regex(@"(?:\*(?<client>\w+))|(?:/(?<company>\w+))",RegexOptions.Compiled);
string input = "*A/AB(M!@12:6?SIMPLE!+5+2";
foreach (Match match in regex.Matches (input)) {
if (match.Groups["client"].Success) {
Console.WriteLine("Client = {0}", match.Groups["client"].Value);
} else if (match.Groups["company"].Success) {
Console.WriteLine("Company = {0}", match.Groups["company"].Value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道正则表达式的语法起初似乎很难理解,但是只要你需要进行这种文本操作,它们就是一个非常强大的工具.
此外,还有一些工具可以帮助您实验并帮助您编写正则表达式,如Expresso和The Regulator.