英文:
C# Regex expression doesn't match string
问题
无法为C#中的字符串构建正确的正则表达式
我有一个看起来像这样的字符串:
string str1 = "f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
我试图构建一个正则表达式,以匹配上述字符串,但需要满足以下条件:
(我将字符串分割成子字符串以更好地解释我需要的内容)
f1- 这是永远不会改变的文本
"9999" - 应该始终是数字,该子字符串的长度可以从2到6个字符不等
<br/>
"d1" - 这是永远不会改变的文本
"310822" - 应该始终是数字,该子字符串的长度可以从2到6个字符不等
<br/>
"orig1" - 这是永远不会改变的文本
<br/>
"GYD" - 应该始终是A-Z字符,长度始终为3
dest1- 这是永远不会改变的文本
<br/>
"SAW" - 应该始终是A-Z字符,长度始终为3
<br/>
"f2" - 这是永远不会改变的文本
<br/>
"7777" - 应该始终是数字,该子字符串的长度可以从2到6个字符不等
<br/>
"d2" - 这是永远不会改变的文本
<br/>
"300922" - 应该始终是数字,该子字符串的长度可以从2到6个字符不等
<br/>
"orig2" - 这是永远不会改变的文本
<br/>
"SAW" - 应该始终是A-Z字符,长度始终为3
<br/>
"dest2"- 这是永远不会改变的文本
<br/>
"GYD" - 应该始终是A-Z字符,长度始终为3
同时,所有斜杠应该放在正确的位置。
我尝试构建以下正则表达式,但它不起作用:
string str1 = "f1/123456/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
if (Regex.IsMatch(str1, @"^[f1][/]\d{2,6}[/][d1][/]\d{2,6}[/][orig1][/]\b[a-zA-Z]{3}\b[/][dest1][/]\b[a-zA-Z]{3}\b[/][f2][/]\d{2,6}[/][d2][/]\d{2,6}[/][orig2][/]\b[a-zA-Z]{3}\b[/][dest2][/]\b[a-zA-Z]{3}\b$"))
{
richTextBox1.Text = "true";
}
else { richTextBox1.Text = "false"; }
我总是得到 "false"。有没有办法为我的字符串str1构建正确的正则表达式?
英文:
Could not build correct regex expression for the string in C#
I have a string that looks like this:
string str1 = "f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
I am trying to build an regex expression that will match string above but with following conditions:
(i will split string above into substrings for better explanation what I need)
f1- is text that never changing
"9999" should always be a digits and length of this substring may vary from 2-6 characters
<br/>
"d1"- this text that never changing
"310822" - should always be a digits and length of this substring may vary from 2-6 characters
<br/>
"orig1" - is text that never changing
<br/>
"GYD" -should be always be a A-Z characters, length always 3
dest1- this text never changing
<br/>
"SAW" -should be always be a A-Z characters, length always 3
<br/>
"f2" -this text that never changing
<br/>
"7777" - should always be a digits and length of this substring may vary from 2-6 characters
<br/>
"d2" - this text never changing
<br/>
"300922" -should always be a digits and length of this substring may vary from 2-6 characters
<br/>
"orig2" - this text never changing
<br/>
"SAW" - should be always be a A-Z characters, length always 3
<br/>
"dest2"-this text never changing
<br/>
"GYD" -should be always be a A-Z characters, length always 3
Also all slashes should be on the right places.
I've tried to build regex expression like below but it doesn't work:
string str1 = "f1/123456/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
if (Regex.IsMatch(str1, @"^[f1][/]\d{2,6}[/][d1][/]\d{2,6}[/][orig1][/]\b[a-zA-Z]{3}\b[/][dest1][/]\b[a-zA-Z]{3}\b[/][f2][/]\d{2,6}[/][d2][/]\d{2,6}[/][orig2][/]\b[a-zA-Z]{3}\b[/][dest2][/]\b[a-zA-Z]{3}\b$"))
{
richTextBox1.Text = "true";
}
else { richTextBox1.Text = "false"; }
I always get "false"
Any idea how to build correct regex for my string str1 ?
答案1
得分: 2
翻译好的部分如下:
string str1 = "f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
string pattern = @"^f1/(?<f1>\d{2,6})/d1/(?<num1>\d{2,6})/orig1/(?<alpha1>[A-Z]{3})/dest1/(?<alpha2>[A-Z]{3})/f2/(?<d1>\d{2,6})/d2/(?<num2>\d{2,6})/orig2/(?<alpha3>[A-Z]{3})/dest2/(?<alpha4>[A-Z]{3})$";
Match match = Regex.Match(str1, pattern);
string f1 = match.Groups["f1"].Value;
string num1 = match.Groups["num1"].Value;
string alpha1 = match.Groups["alpha1"].Value;
string alpha2 = match.Groups["alpha2"].Value;
string d1 = match.Groups["d1"].Value;
string num2 = match.Groups["num2"].Value;
string alpha3 = match.Groups["alpha3"].Value;
string alpha4 = match.Groups["alpha4"].Value;
英文:
Try following
string str1 = "f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
string pattern = @"^f1/(?'f1'\d{2,6})/d1/(?'num1'\d{2,6})/orig1/(?'alpha1'[A-Z]{3})/dest1/(?'alpha2'[A-Z]{3})/f2/(?'d1'\d{2,6})/d2/(?'num2'\d{2,6})/orig2/*(?'alpha3'[A-Z]{3})/dest2/(?'alpha4'[A-Z]{3})$";
Match match = Regex.Match(str1, pattern);
string f1 = match.Groups["f1"].Value;
string num1 = match.Groups["num1"].Value;
string alpha1 = match.Groups["alpha1"].Value;
string alpha2 = match.Groups["alpha2"].Value;
string d1 = match.Groups["d1"].Value;
string num2 = match.Groups["num2"].Value;
string alpha3 = match.Groups["alpha3"].Value;
string alpha4 = match.Groups["alpha4"].Value;
答案2
得分: 0
这个表达式将针对你的字符串返回"true"。
英文:
This expression will return "true" for your string
string str1 = "f1/123456/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";
if (Regex.IsMatch(str1, @"^[f][1][/]\d{2,6}[/][d][1][/]\d{2,6}[/][o][r][i][g][1][/]\b[a-zA-Z]{3}\b[/][d][e]展开收缩[t][1][/]\b[a-zA-Z]{3}\b[/][f][2][/]\d{2,6}[/][d][2][/]\d{2,6}[/][o][r][i][g][2][/]\b[a-zA-Z]{3}\b[/][d][e]展开收缩[t][2][/]\b[a-zA-Z]{3}\b$"))
{
richTextBox1.Text = "true";
}
else { richTextBox1.Text = "false"; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论