简体   繁体   English

在 c# 中使用 Regex.Split

[英]Using Regex.Split in c#

So i have a string that contains snowflake columns and i want to split the string to each column, I'm trying to use Regex to do this as it split string won't work in this situation.所以我有一个包含雪花列的字符串,我想将字符串拆分为每一列,我正在尝试使用 Regex 来执行此操作,因为拆分字符串在这种情况下不起作用。 The string pattern i have tried is我试过的字符串模式是

string pattern = @"([^\s]*\s[^\s]*),"

though this pattern splits after the second consecutive space.尽管此模式在第二个连续空格后分裂。 Im not sure how to split it just after the alias.我不确定如何在别名之后拆分它。 I am also using .net core 3.1.我也在使用 .net 核心 3.1。 Any help would be appreciated..任何帮助,将不胜感激..

current snowflake datacolumn string:当前雪花数据列字符串:

string columns = "nvl(u.\"Country\",'#N/A') \"Country\",u.\"CreatedDate\" \"CreatedDate\",nvl(u.\"Email\",'#N/A') \"Email\",u.\"LastModifiedDate\" \"LastModifiedDate\",nvl(u.\"Name\",'#N/A') \"Name\"";

expected output:预计 output:
nvl(u."Country",'#N/A') "Country" nvl(u."国家",'#N/A') "国家"
u."CreatedDate" "CreatedDate" u."创建日期" "创建日期"
nvl(u."Email",'#N/A') "Email" nvl(u."电子邮件",'#N/A') "电子邮件"
u."LastModifiedDate" "LastModifiedDate" u."LastModifiedDate" "LastModifiedDate"
nvl(u."Name",'#N/A') "Name" nvl(u."姓名",'#N/A') "姓名"

You can use您可以使用

string[] result = Regex.Split(text, @"(?<=\s""\w+""),");

See the .NET regex demo .请参阅.NET 正则表达式演示 Details :详情

  • (?<=\s"\w+") - a positive lookbehind that matches a location immediately preceded with a whitespace, " , one or more word chars, " (?<=\s"\w+") - 正向后视匹配紧跟在空格前面的位置, " ,一个或多个单词字符, "
  • , - a comma. , - 逗号。

在此处输入图像描述

Another idea is to extract the matches with另一个想法是提取匹配项

var result = Regex.Matches(text, @"\b(?:nvl\([^()]*\)|u\.""[^""]*"")\s+""[^""]*""")
    .Cast<Match>()
    .Select(x => x.Value);

See this regex demo .请参阅此正则表达式演示

在此处输入图像描述

Details :详情

  • \b - word boundary \b - 单词边界
  • (?:nvl\([^()]*\)|u\."[^"]*") - nvl(...) or u."..." (?:nvl\([^()]*\)|u\."[^"]*") - nvl(...)u."..."
  • \s+ - one or more whitespaces \s+ - 一个或多个空格
  • "[^"]*" - " , zero or more non- " s, and a " . "[^"]*" - " ,零个或多个非" s,和一个"

You can use a capture group (group 1) and exclude the comma in the second part after matching the space.您可以使用捕获组(第 1 组)并在匹配空格后排除第二部分中的逗号。 To match all parts, you can match either a comma or the end of the string at the end of the pattern.要匹配所有部分,您可以匹配模式末尾的逗号或字符串末尾。

This part [^\s]* can be written as \S*这部分[^\s]*可以写成\S*

(\S*\s[^\s,]*)(?:,|$)
  • ( Capture group 1 (捕获组 1
    • \S*\s[^\s,]* Match optional non whitespace chars, match a whitespace char and match optional non whitespace chars except a comme \S*\s[^\s,]*匹配可选的非空白字符,匹配空白字符并匹配可选的非空白字符,comme 除外
  • ) Close group 1 )关闭组 1
  • (?:,|$) Match either a comma or assert the end of the string (?:,|$)匹配逗号或断言字符串结尾

.NET regex demo .NET 正则表达式演示

在此处输入图像描述

For example例如

string pattern = @"(\S*\s[^\s,]*)(?:,|$)";
string input = @"nvl(u.""Country"",'#N/A') ""Country"",u.""CreatedDate"" ""CreatedDate"",nvl(u.""Email"",'#N/A') ""Email"",u.""LastModifiedDate"" ""LastModifiedDate"",nvl(u.""Name"",'#N/A') ""Name""";

foreach (Match m in Regex.Matches(input, pattern))
{
    Console.WriteLine(m.Groups[1].Value);
}

Output Output

nvl(u."Country",'#N/A') "Country"
u."CreatedDate" "CreatedDate"
nvl(u."Email",'#N/A') "Email"
u."LastModifiedDate" "LastModifiedDate"
nvl(u."Name",'#N/A') "Name"

A bit more specific pattern using + to match 1 or more characters and match word characters between double quotes:使用+匹配 1 个或多个字符并匹配双引号之间的单词字符的更具体的模式:

 (\S+\s"\w+")(?:,|$)

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM