正则表达式在编辑器扩展中爆炸
我正在为我的 Django 渲染引擎构建 Visual Studio 编辑器扩展。 我刚刚开始使用它,到目前为止它非常简单,并且到目前为止它达到了我期望的效果 - 突出显示等等。 或者直到我开始添加解析逻辑为止。 部分解析依赖于正则表达式。 这是我的问题: 无论我如何尝试 - 静态变量,成员变量, - 任何东西,每次我调用 new Regex 时,它都会给我“对象未设置为实例”异常。 在MEF中使用正则表达式(RegEx)有问题吗?
就这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
namespace NDjango.Designer.Parsing
{
public interface IParser
{
List<Token> Parse(IEnumerable<string> template);
}
[Export(typeof(IParser))]
public class Parser : IParser
{
public List<Token> Parse(IEnumerable<string> template)
{
var result = new List<Token>();
Regex tag_re = new Regex("({{.*}}", RegexOptions.Compiled);
return result;
}
}
}
正则表达式构造函数所在行的断点被命中。 接下来的 F10 给出了例外
I am building a Visual Studio editor extension for my Django rendering engine. I just started it so so far it is really simple and so far it does what I expect it to do - highlighting and the such.
Or it did until I started to add parsing logic. Part of the parsing relies on regular expressions. And here is my problem:
No matter how I try - static variables, member variables, - anything, every time I call new Regex it gives me "Object not set to an instance" exception.
Is there a problem with using regular expressions (RegEx) in MEF?
here you go:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
namespace NDjango.Designer.Parsing
{
public interface IParser
{
List<Token> Parse(IEnumerable<string> template);
}
[Export(typeof(IParser))]
public class Parser : IParser
{
public List<Token> Parse(IEnumerable<string> template)
{
var result = new List<Token>();
Regex tag_re = new Regex("({{.*}}", RegexOptions.Compiled);
return result;
}
}
}
A breakpoint on the line with the Regex constructor is hit just fine. The next F10 gives the exception
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,在 MEF 中使用正则表达式没有问题。 问题出在你的代码中。
根据错误消息,当您尝试创建新的正则表达式时,您似乎遇到了空引用异常。 如果不查看代码示例,就很难知道到底出了什么问题。
您可以发布代码示例吗?
No, there is no issue with using regular expressions in MEF. The issue is in your code.
Based on the error message it looks like you are hitting a null reference exception when you try and create a new regex. Without seeing a code sample it's not easy to know what exactly is going wrong.
Can you post a code sample?
对不起,伙计们,我想通了,JaredPar,你是对的 - 问题是正则表达式中的 paren 不平衡。 至少可以说,“诊断”是安静的误导
I am sorry guys, I figured it out, JaredPar you were right - the problem was unbalanced paren in the regexp. The "diagnostic" though was quiet misleading to say the least