如何在 .NET 中解析文本并容忍空白以查找特定功能或模式?

发布于 2025-01-06 07:07:04 字数 992 浏览 4 评论 0原文

我想找到一些最佳实践方法,如何解析文本块,寻找类似函数的文本,并容忍文本中的空白,而不必仅针对这种情况进行配置和利用 ANTLR(或类似的东西)之类的操作。在解析中唯一要寻找的是某个特定项目的变化。

示例:在下面的示例中,我想了解“文本”、“必需”和“类型”(如果存在)的值是什么。他们可能并不全部都在场。在文本块 1 和 2 中,我想知道当解析出感兴趣的值时,两个项目的值是否相同。 Text 是“标签文本是什么”,Required 为 true,Type 为 null 或未指定,同时忽略或宽恕所有间距。

1. This is user entered text. {{Text="What is the label text {{ Ignore me and treat me as text }}?",Required=true              }}. I am some more user added {} text. 
2. This is user entered text. {{ Text     ="What is the label text {{ Ignore me and treat me as text }}?", Required=       True}}. I am some more user added {} text. 
3. This is user entered text. {{Text="What is the label text?",Type=Date}}. I am some more user added {} text. 
4. This is user entered text. {{ Text = "What is the label text?", Type = Date }}I am some more user added {} text. 
5. This is user entered text. {{ Text = "What is the label text?", Type = Date, Required = True }}I am some more user added {} text. 

I wanted to find out some best practice methods how to parse text blocks looking for function-like text and be forgiving of white space within the text without having to do something like configure and utilize ANTLR (or something similar) just for this one case. The only thing that will be sought after in the parsing are variations on one particular item.

Example: In the example below I'd like to find out what the value is for Text, Required, and Type if they are present. They may not all be present. In text blocks 1 and 2, I would want to know that the values on both items are the same when the interested values are parsed out. Text is "What is the label text", that Required is true, and that Type is null or not specified while ignoring or forgiving all the spacing.

1. This is user entered text. {{Text="What is the label text {{ Ignore me and treat me as text }}?",Required=true              }}. I am some more user added {} text. 
2. This is user entered text. {{ Text     ="What is the label text {{ Ignore me and treat me as text }}?", Required=       True}}. I am some more user added {} text. 
3. This is user entered text. {{Text="What is the label text?",Type=Date}}. I am some more user added {} text. 
4. This is user entered text. {{ Text = "What is the label text?", Type = Date }}I am some more user added {} text. 
5. This is user entered text. {{ Text = "What is the label text?", Type = Date, Required = True }}I am some more user added {} text. 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2025-01-13 07:07:04

由于我在您的示例中没有看到嵌套,因此正则表达式即可。

Since I see no nesting in your examples, regular expressions will do.

满栀 2025-01-13 07:07:04

使用三个正则表达式的组合。

Text\s*=\s*\"(?.*)\".*Type\s*=\s*(?\w*).*必填\s*当所有三个部分都存在时 =\s*(?\w*) 将匹配。

Text\s*=\s*\"(?.*)\".*Type\s*=\s*(?\w*) 将匹配当缺少“必需”时。

Text\s*=\s*\"(?.*)\".*Required\s*=\s*(?\w*) 将匹配当“类型”丢失时。

使用可以使用此 在线尝试这些表达式正则表达式解析器

Use a combination of three regexes.

Text\s*=\s*\"(?<Text>.*)\".*Type\s*=\s*(?<Type>\w*).*Required\s*=\s*(?<Required>\w*) will match when all three parts are present.

Text\s*=\s*\"(?<Text>.*)\".*Type\s*=\s*(?<Type>\w*) will match when 'Required' is missing.

Text\s*=\s*\"(?<Text>.*)\".*Required\s*=\s*(?<Required>\w*) will match when 'Type' is missing.

Use can try these expressions out using this online regex parser.

苦行僧 2025-01-13 07:07:04
var re = new Regex(@"(?i){{(?:[\s,]*(?:Text\s*=\s*""((?:[^""\\]|\\.)+)""|Type\s*=\s*(\w+)|Required\s*=\s*(\w+)))+");
foreach (Match m in re.Matches(text)) {
    Console.WriteLine("Text: {0}\nType: {1}\nRequired: {2}\n", m.Groups[1], m.Groups[2], m.Groups[3]);
}

Text、Required 和 Type 可以采用任意顺序并且是可选的。
此正则表达式假定文本内可以存在转义序列。

var re = new Regex(@"(?i){{(?:[\s,]*(?:Text\s*=\s*""((?:[^""\\]|\\.)+)""|Type\s*=\s*(\w+)|Required\s*=\s*(\w+)))+");
foreach (Match m in re.Matches(text)) {
    Console.WriteLine("Text: {0}\nType: {1}\nRequired: {2}\n", m.Groups[1], m.Groups[2], m.Groups[3]);
}

Text, Required and Type can be in any order and are optional.
This regex assumes that there can be escaped sequences inside Text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文