C# 从给定条件的字符串数组中提取字符串,即类';s属性
用例:我导入文本文件,需要读取由4行组成的内容。通过这4行,我解析出了不是预定义的,而是动态的字符串 下面是我从@zx81收集的一个示例: 输入:C# 从给定条件的字符串数组中提取字符串,即类';s属性,c#,arrays,regex,linq,C#,Arrays,Regex,Linq,用例:我导入文本文件,需要读取由4行组成的内容。通过这4行,我解析出了不是预定义的,而是动态的字符串 下面是我从@zx81收集的一个示例: 输入: on Apr 28, 2014 at 22:00 an Employee John Doe accessed server - TPCX123 AccessType2 was ReasonType1 - program: Px2x3x, start: No22, 0.0 sec ReportDate = Apr 28, 2014 ReportTim
on Apr 28, 2014 at 22:00
an Employee John Doe accessed
server - TPCX123
AccessType2 was ReasonType1 - program: Px2x3x, start: No22, 0.0 sec
ReportDate = Apr 28, 2014
ReportTime = 22:00
EmployeeName = John Doe
ServerName = TnCX123
AccessType = AccessType2
ReasonType = ReasonType1
ProgramId = Px2x3x
Start = No22
Length = 0.0 sec
因此,考虑到上面的4行,我想要么将它们与回车一起保存(即4行),要么将它们全部变成一个字符串(即仅一行),我将提取属性,并通过类的属性将它们放入内存,例如报告日期
,报告时间
,员工姓名
,ServerName
,AccessType
,ReasonType
,programmaid
,开始
,长度
所需输出:
on Apr 28, 2014 at 22:00
an Employee John Doe accessed
server - TPCX123
AccessType2 was ReasonType1 - program: Px2x3x, start: No22, 0.0 sec
ReportDate = Apr 28, 2014
ReportTime = 22:00
EmployeeName = John Doe
ServerName = TnCX123
AccessType = AccessType2
ReasonType = ReasonType1
ProgramId = Px2x3x
Start = No22
Length = 0.0 sec
这就是我想要的—在等号的RHS上找到的所有项,即分配给内存中对象
中找到的特定属性的某些字符串,它们最终响应数据库表的列。从上面的示例中,属性EmployeeName
将始终位于同一位置(在特定字符串之间),因此将解析出其值,例如“John Doe”。当然,对于我引入的每个文件,这些值都是不同的,因此是它的动态部分
希望这能有所帮助,谢谢。根据您的数据,类似这样的东西会输出您想要的:
输出:
ReportDate = Apr 28, 2014
ReportTime = 22:00
EmployeeName = John Doe
ServerName = TnCX123
AccessType = AccessType2
ReasonType = ReasonType1
ProgramId = Px2x3x
Start = No22
Length = 0.0 sec
using System;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
class Program
{
static void Main()
{
string s1 = @"on Apr 28, 2014 at 22:00
an Employee John Doe accessed
server - TPCX123
AccessType2 was ReasonType1 - program: Px2x3x, start: No22, 0.0 sec";
try
{
var myRegex = new Regex(@"(?s)^on\s+([\w, ]+?) at (\d{2}:\d{2}).*?Employee ([\w ]+) accessed.*?server - (\w+).*?(\w+) was (\w+) - program: (\w+), start: (\w+), (\d+\.\d+ \w+)");
string date = myRegex.Match(s1).Groups[1].Value;
string time = myRegex.Match(s1).Groups[2].Value;
string name = myRegex.Match(s1).Groups[3].Value;
string server = myRegex.Match(s1).Groups[4].Value;
string access = myRegex.Match(s1).Groups[5].Value;
string reason = myRegex.Match(s1).Groups[6].Value;
string prog = myRegex.Match(s1).Groups[7].Value;
string start = myRegex.Match(s1).Groups[8].Value;
string length = myRegex.Match(s1).Groups[9].Value;
Console.WriteLine("ReportDate = " + date);
Console.WriteLine("ReportTime = " + time);
Console.WriteLine("EmployeeName = " + name);
Console.WriteLine("ServerName = " + server);
Console.WriteLine("AccessType = " + access);
Console.WriteLine("ReasonType = " + reason);
Console.WriteLine("ProgramId = " + prog);
Console.WriteLine("Start = " + start);
Console.WriteLine("Length = " + length);
}
catch (ArgumentException ex)
{
// We have a syntax error
}
Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();
} // END Main
} // END Program
代码:
ReportDate = Apr 28, 2014
ReportTime = 22:00
EmployeeName = John Doe
ServerName = TnCX123
AccessType = AccessType2
ReasonType = ReasonType1
ProgramId = Px2x3x
Start = No22
Length = 0.0 sec
using System;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
class Program
{
static void Main()
{
string s1 = @"on Apr 28, 2014 at 22:00
an Employee John Doe accessed
server - TPCX123
AccessType2 was ReasonType1 - program: Px2x3x, start: No22, 0.0 sec";
try
{
var myRegex = new Regex(@"(?s)^on\s+([\w, ]+?) at (\d{2}:\d{2}).*?Employee ([\w ]+) accessed.*?server - (\w+).*?(\w+) was (\w+) - program: (\w+), start: (\w+), (\d+\.\d+ \w+)");
string date = myRegex.Match(s1).Groups[1].Value;
string time = myRegex.Match(s1).Groups[2].Value;
string name = myRegex.Match(s1).Groups[3].Value;
string server = myRegex.Match(s1).Groups[4].Value;
string access = myRegex.Match(s1).Groups[5].Value;
string reason = myRegex.Match(s1).Groups[6].Value;
string prog = myRegex.Match(s1).Groups[7].Value;
string start = myRegex.Match(s1).Groups[8].Value;
string length = myRegex.Match(s1).Groups[9].Value;
Console.WriteLine("ReportDate = " + date);
Console.WriteLine("ReportTime = " + time);
Console.WriteLine("EmployeeName = " + name);
Console.WriteLine("ServerName = " + server);
Console.WriteLine("AccessType = " + access);
Console.WriteLine("ReasonType = " + reason);
Console.WriteLine("ProgramId = " + prog);
Console.WriteLine("Start = " + start);
Console.WriteLine("Length = " + length);
}
catch (ArgumentException ex)
{
// We have a syntax error
}
Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();
} // END Main
} // END Program
调整它
然而,要调整它,你必须刷新你的正则表达式
首先,这里是代码中正则表达式的一个令牌一个令牌的解释。然后我建议您访问,以及常见问题解答中提到的其他网站
@"
(? # Use these options for the whole regular expression
s # Dot matches line breaks
)
^ # Assert position at the beginning of the string
on # Match the character string “on” literally (case sensitive)
\s # Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( # Match the regex below and capture its match into backreference number 1
[\w,\ ] # Match a single character present in the list below
# A “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
# A single character from the list “, ”
+? # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
)
\ at\ # Match the character string “ at ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 2
\d # Match a single character that is a “digit” (0–9 in any Unicode script)
{2} # Exactly 2 times
: # Match the character “:” literally
\d # Match a single character that is a “digit” (0–9 in any Unicode script)
{2} # Exactly 2 times
)
. # Match any single character
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Employee\ # Match the character string “Employee ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 3
[\w\ ] # Match a single character present in the list below
# A “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
# The literal character “ ”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ accessed # Match the character string “ accessed” literally (case sensitive)
. # Match any single character
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
server\ -\ # Match the character string “server - ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 4
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
. # Match any single character
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
( # Match the regex below and capture its match into backreference number 5
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ was\ # Match the character string “ was ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 6
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ -\ program:\ # Match the character string “ - program: ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 7
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
,\ start:\ # Match the character string “, start: ” literally (case sensitive)
( # Match the regex below and capture its match into backreference number 8
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
,\ # Match the character string “, ” literally
( # Match the regex below and capture its match into backreference number 9
\d # Match a single character that is a “digit” (0–9 in any Unicode script)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. # Match the character “.” literally
\d # Match a single character that is a “digit” (0–9 in any Unicode script)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\ # Match the character “ ” literally
\w # Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"
我明白你们为什么想要狗,但为什么要围栏?你们不能只用绳子吗。分开(“”)?这将为您提供一个包含所有单词的数组。您需要哪些字符串的条件是什么?感谢所有回复的人。我已经更新了我原来的帖子,希望它能回答你的问题。请参考“编辑1”标签下的新部分。再次感谢@user118190我完全修改了我的答案,以匹配您提供的新数据。:)太神了感谢@zx81对理解解决方案及其背后的概念所作的非常有用的解释@user118190嘿,不客气,很高兴听到它起作用。感谢您的友好反馈。:)顺便说一句,您应该知道,我并没有手工编写逐个令牌的解释:我要求regexbuddy生成它。如果你要做你看起来要做的事情,那可能是一个很有价值的工具。