Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,正则表达式_C#_Regex - Fatal编程技术网

C#,正则表达式

C#,正则表达式,c#,regex,C#,Regex,如何使用正则表达式拆分以下字符串: Product : Volks, Wagon PurchasedDate: 16/09/2016 Quantity: 70 IsVehicle: Y Telephone: 9603 8303 (H) Your Reference : 111 Our Reference : 08512781112 Phone Enquiries: Acct. Alan Donald 例如: Product: Vol

如何使用正则表达式拆分以下字符串:

Product  :      Volks, Wagon      PurchasedDate: 16/09/2016    Quantity: 70    
IsVehicle: Y   Telephone: 9603 8303 (H) Your Reference :  111 Our Reference  
:   08512781112  Phone Enquiries:      Acct. Alan Donald
例如:

Product: Volks, Wagon      
PurchasedDate: 16/09/2016    
Quantity: 70    
IsVehicle: Y   
Telephone: 9603 8303 (H)
Your Reference: 111 
Our Reference: 08512781112  
Phone Enquiries: Acct. Alan Donald
我尝试了类似的方法,但没有产生我想要的结果:

var regexString = Regex.Split(textString, @"([a-zA-Z]*)[\r]*:([a-zA-Z]*)[\r]*");
任何帮助都将不胜感激。
“p

替代
$1:$2\n$3:$4\n$5:$6\n$7:$8\n$9:$10\n$11:$12\n$13:$14\n$15:$16

或者您可以使用:


Regex
(产品|采购日期|数量| IsVehicle |电话|您的参考|我们的参考|电话查询|*:\s*(\w+,\s\w+| \d+\/\d+\/\d+|)(?:Y | N)\d+\s\d++(H\)\d+\s*

替换
$1:$2\n


由于输入字符串不规则(如何知道哪些空格是空格,哪些是分隔符!?),因此没有特别可读的模式

在考虑这些特殊情况时,尽量保持模式的通用性和简单性,然后:

模式:
(.*)\s*:\s*(.*)((?=我们的|你的)|\s{2,}|$)

替换:
\1:\2\n

您可以匹配它,而不是拆分:

(?<key>\b(?:(?!\s{2,})[^:])+\b)\s*:\s*
(?<value>\b(?:(?!(?:Y?[Oo]ur\ Reference)|\s{2,})[^:])+\b)
(?\b(?:(?!\s{2,})[^:]+\b)\s*:\s*
(?\b(?:(?!(?:Y?[Oo]ur\Reference)|\s{2,})[^:]+\b)
请参阅。

尝试以下操作:

var s = "Product...";
string pattern =
    @"(?s)(\s+)(Product|PurchasedDate|Quantity|IsVehicle|" +
    @"Telephone|Your Reference|Our Reference|Phone Enquiries)(\s*?(\r\n)*?\s*?:)";
string s2 = Regex.Replace(Regex.Replace(s, pattern, "\r\n$2:"), @"\u0020{2,}", "\u0020");

它可以用分号分隔,但您有带空格的关键字。关键词会一直和这篇文章一样吗?是的。与文本中相同,比我的简单,显然+1