如何在 .NET 中解析文本并容忍空白以查找特定功能或模式?
我想找到一些最佳实践方法,如何解析文本块,寻找类似函数的文本,并容忍文本中的空白,而不必仅针对这种情况进行配置和利用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于我在您的示例中没有看到嵌套,因此正则表达式即可。
Since I see no nesting in your examples, regular expressions will do.
使用三个正则表达式的组合。
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.
Text、Required 和 Type 可以采用任意顺序并且是可选的。
此正则表达式假定文本内可以存在转义序列。
Text, Required and Type can be in any order and are optional.
This regex assumes that there can be escaped sequences inside Text.