.NET 语句的表达式

发布于 2024-11-07 03:21:46 字数 1045 浏览 0 评论 0原文

我想为我的个人用途实现一个在线编辑器,因此我必须检查我在在线文本编辑器中编写的语句是否采用正确的形式或不

我想用正则表达式检查语句的形式,我找到了一个很好的IF语句表达式 这里现在

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*;

我正在寻找表达式的其余部分,例如:

  • for
  • while
  • 变量定义(如:int xxx string xxx)
  • switch case
  • new(object:class,enum)
  • foreach

不要说这是不可能的,我可以使用上面提到的正则表达式IF表达式来处理我的问题效果很好 剩下的我需要这样的东西,

提前谢谢你

I want to implement an online editor for my personal usages, so that I have to check that the statements which I write in my online text editor are in right form or not.

I want to check the form of statements with regular expressions, I found a good expression for IF statement here and it is

if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?1)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?2)})*+)}\s*(?:(?:else\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?3)})*+)}\s*)|(?:else\s*if\s*\(((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^\(\)]|\((?4)\))*+)\)\s*{((?:(?:(?:"(?:(?:\\")|[^"])*")|(?:'(?:(?:\\')|[^'])*'))|[^{}]|{(?5)})*+)}\s*))*;

Now I'm looking for the rest of expression e.g:

  • for
  • while
  • variable definition (like : int xxx string xxx)
  • switch case
  • new (object : class, enum)
  • foreach

Please don't say that it's impossible, I can handle my problem with regular expression and IF expression I mentioned above is working great and I need such a thing for the rest

Thank you in advance

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

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

发布评论

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

评论(3

尹雨沫 2024-11-14 03:21:46

尽管这不是您问题的直接答案 - 我坚持您使用ANTLR< /a> 相反

Even though this is not a direct answer to your question - I'm insisting you to use ANTLR instead

柠檬心 2024-11-14 03:21:46

在这种情况下使用正则表达式不是一个好主意,您应该使用类似的东西
语法分析器

It is not a good idea to use in this case regular expressions, you should use something like
syntax analyzer

像你 2024-11-14 03:21:46

我使用正则表达式进行解析的经验是,它们只有在满足以下条件时才实用:

  • 您愿意自己编写和维护它们
  • 您可以接受它们并不严格正确

因为您正在寻求帮助,所以您可能不符合第一个标准并且所以我不能为你推荐它们。

PS 是的,我有时确实会在快速而肮脏的解析情况下使用它们。它们是无价的。

My experience with using regular expressions for parsing is they are only practical if:

  • You are willing to write and maintain them yourself
  • You can accept that they aren't rigorously correct

Since you are asking for help you probably don't meet the first criterion and so I cannot recommend them for you.

P.S. Yes, I sometimes do use them myself in quick-and-dirty parsing situations. They are invaluable.

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