无法为C#中的字符串构建正确的正则表达式

我有一个看起来像这样的字符串:

 string str1 = "f1/9999/d1/310822/orig1/GYD/dest1/SAW/f2/7777/d2/300922/orig2/SAW/dest2/GYD";

我正在try 构建一个正则表达式,该表达式将与上面的字符串匹配,但满足以下条件:

(我会将上面的字符串拆分成子字符串,以便更好地解释我需要的内容)

f1- is text that never changing "9999" should always be a digits and length of this substring may vary from 2-6 characters
"d1"- this text that never changing "310822" - should always be a digits and length of this substring may vary from 2-6 characters
"orig1" - is text that never changing
"GYD" -should be always be a A-Z characters, length always 3 dest1- this text never changing
"SAW" -should be always be a A-Z characters, length always 3
"f2" -this text that never changing
"7777" - should always be a digits and length of this substring may vary from 2-6 characters
"d2" - this text never changing
"300922" -should always be a digits and length of this substring may vary from 2-6 characters
"orig2" - this text never changing
"SAW" - should be always be a A-Z characters, length always 3
"dest2"-this text never changing
"GYD" -should be always be a A-Z characters, length always 3

另外,所有的斜杠都应该放在正确的位置.

我曾try 构建如下所示的正则表达式,但不起作用:

 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"; }

我总是得到"假的". 您知道如何为字符串str1构建正确的正则表达式吗?​

推荐答案

该表达式将为您的字符串返回"true"

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][s][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][s][t][2][/]\b[a-zA-Z]{3}\b$"))
            
            {
                richTextBox1.Text = "true";
            }
            else { richTextBox1.Text = "false"; }​

Csharp相关问答推荐

我无法在Program.cs中实例化我的学生类

SortedSet.IsSubsetOf未按预期工作

Umbraco Lucene日期字段编码转换为.net日期时间

需要深入了解NpgSQL DateTimeOffset处理

获取ASP.NET核心身份认证cookie名称

有没有办法把+02:00转换成TimeSpan?""

如何在不考虑年份的情况下判断日期时间是否在某个日期范围内?

C#XmlSerializer-输出控制新行的多个XML片段

在ASP.NET Core Web API项目中通过中间件修改`Request.Path`不会更改使用的控制器/操作

如何将此方法参数化并使其更灵活?

如果设置了另一个属性,则Newtonsoft JSON忽略属性

在C#中,非静态接口方法的抽象和虚拟是冗余的吗?

如何比较C#中的L和ł(波兰字符)返回TRUE

Selify只更改第一个下拉菜单,然后忽略REST-C#

自定义列表按字符串的部分排序

.NET 6:如何防止系统生成的日志(log)?

如何避免在.NET中将日志(log)写入相对路径

如何使用EPPlus C#在单个单元格中可视化显示多行文字

Autofac -动态实例化:手动传递构造函数

使用C#12中的主构造函数进行空判断