C#正则表达式无法匹配字符串。

huangapple go评论77阅读模式
英文:

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 = &quot;f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD&quot;;

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 = &quot;f1/123456/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD&quot;;
        if (Regex.IsMatch(str1, @&quot;^[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$&quot;))
        {
            richTextBox1.Text = &quot;true&quot;;
        }
        else { richTextBox1.Text = &quot;false&quot;; }

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 = &quot;f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD&quot;;
string pattern = @&quot;^f1/(?&#39;f1&#39;\d{2,6})/d1/(?&#39;num1&#39;\d{2,6})/orig1/(?&#39;alpha1&#39;[A-Z]{3})/dest1/(?&#39;alpha2&#39;[A-Z]{3})/f2/(?&#39;d1&#39;\d{2,6})/d2/(?&#39;num2&#39;\d{2,6})/orig2/*(?&#39;alpha3&#39;[A-Z]{3})/dest2/(?&#39;alpha4&#39;[A-Z]{3})$&quot;;
Match match = Regex.Match(str1, pattern);
string f1 = match.Groups[&quot;f1&quot;].Value;
string num1 = match.Groups[&quot;num1&quot;].Value;
string alpha1 = match.Groups[&quot;alpha1&quot;].Value;
string alpha2 = match.Groups[&quot;alpha2&quot;].Value;
string d1 = match.Groups[&quot;d1&quot;].Value;
string num2 = match.Groups[&quot;num2&quot;].Value;
string alpha3 = match.Groups[&quot;alpha3&quot;].Value;
string alpha4 = match.Groups[&quot;alpha4&quot;].Value;

答案2

得分: 0

这个表达式将针对你的字符串返回"true"。

英文:

This expression will return "true" for your string

string str1 = &quot;f1/123456/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD&quot;;
            if (Regex.IsMatch(str1, @&quot;^[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$&quot;)) { richTextBox1.Text = &quot;true&quot;; } else { richTextBox1.Text = &quot;false&quot;; }​

huangapple
  • 本文由 发表于 2023年5月28日 08:13:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定