.NET 语句的表达式
我想为我的个人用途实现一个在线编辑器,因此我必须检查我在在线文本编辑器中编写的语句是否采用正确的形式或不。
我想用正则表达式检查语句的形式,我找到了一个很好的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管这不是您问题的直接答案 - 我坚持您使用ANTLR< /a> 相反
Even though this is not a direct answer to your question - I'm insisting you to use ANTLR instead
在这种情况下使用正则表达式不是一个好主意,您应该使用类似的东西
语法分析器
It is not a good idea to use in this case regular expressions, you should use something like
syntax analyzer
我使用正则表达式进行解析的经验是,它们只有在满足以下条件时才实用:
因为您正在寻求帮助,所以您可能不符合第一个标准并且所以我不能为你推荐它们。
PS 是的,我有时确实会在快速而肮脏的解析情况下使用它们。它们是无价的。
My experience with using regular expressions for parsing is they are only practical if:
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.