正则表达式注释多行



我想在 C# 中使用正则表达式进行多行注释。我有@"/[*][wds]+[*]/",但使用该表达式仅注释单行而不是多行中出现的/* */之间的文本。

单行:

       /* xxxxxxxx */

多行:

       /*
       xxxxxxx
       */

我不知道我是否可以很好地解释,但是任何问题或您是否可以参考提供此信息的地方,我将不胜感激。

编辑在我的示例中,我在一个类中

...

    public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
    {
        List<ClassificationSpan> classifications = new List<ClassificationSpan>();
        string current = span.GetText();
        bool commentFound = false;
        foreach(var item in _colorTextoLanguage.Comments)
        {
            Regex reg = new Regex(item, RegexOptions.IgnoreCase);
            var matches = reg.Matches(current);
            for(int i=0;i<matches.Count;i++)
            {
                commentFound = true;
                Match m =matches[i];
                Span new_span = new Span(span.Start.Position + m.Index, current.Length - m.Index);
                SnapshotSpan new_snapshot = new SnapshotSpan(span.Snapshot, new_span);
                var newText = new_snapshot.GetText();
                classifications.Add(new ClassificationSpan(new_snapshot, _commentType));
            }
        }
        if(commentFound)
            return classifications;
        Classify(classifications, current, span, _colorTextoLanguage.Custom, _classificationType);
        Classify(classifications, current, span, _colorTextoLanguage.Quoted, _stringType);
        Classify(classifications, current, span, _colorTextoLanguage.Keywords, _keywordType);
        Classify(classifications, current, span, _colorTextoLanguage.IdentifierTypes, _identifierType);
        Classify(classifications, current, span, _colorTextoLanguage.Numeric, _numericType);
        return classifications;
    }

...

和其他类

class ColorTextoLanguage
{
    #region Member Variables
    private List<string> _comments = new List<string>();
    private List<string> _quoted = new List<string>();
    private List<string> _numeric = new List<string>();
    private List<string> _keywords = new List<string>();
    private List<string> _identiferTypes = new List<string>();
    private List<string> _custom = new List<string>();

    #region Properties
    public List<string> Comments
    {
        get{return _comments;}
    }
    public List<string> Quoted
    {
        get{return _quoted;}
    }
    public List<string> Numeric
    {
        get{return _numeric;}
    }
    public List<string> Keywords
    {
        get{return _keywords;}
    }
    public List<string> IdentifierTypes
    {
        get{return _identifierTypes;}
    }
    public List<string> Custom
    {
        get{return _custom;}
    }
    #endregion
    #region ctor
    public ColorTextoLanguage()
    {
        Initialize();
    }
    #endregion
    #region Methods
    private void Initialize()
    {
        _comments.Add("//");
        _comments.Add(@"/*(?:(?!*/)(?:.|[rn]+))**/");
        _quoted.Add(@"([""'])(?:\1|.)*?1");
        _numeric.Add(@"bd+b")
        _keywords.Add(@"bifb");
        _keywords.Add(@"belseb");
        _keywords.Add(@"bforeachb");
        _keywords.Add(@"bswitchb");
        _keywords.Add(@"bcaseb");
        .
        .
        .

        _identifierTypes.Add(@"bintb");
        _identifierTypes.Add(@"bdateb");
        _identifierTypes.Add(@"bstringb");
        .
        .
        .
    }
    #endregion
    #endregion
};

不确定这是否有帮助,但从我所看到的与您的示例非常相似。谢谢补充

试试正则表达式:

/*(?:(?!*/).)**/

使用正则表达式选项.单行

new Regex(@"/*(?:(?!*/).)**/", RegexOptions.Singleline);

正则表达式101演示

(?:(?!*/).)*将匹配除*/以外的任何字符

编辑:应该在两种模式下工作的版本:

/*(?:(?!*/)(?:.|[rn]+))**/
/*([^*]**)*?/

/*匹配一个正斜杠,后跟一个星号

([^*]**)*?(将所有不是文字星号零的东西匹配到无限次,然后匹配文字星号),懒惰地零次或无限次

/匹配正斜杠。 如果此操作失败,请回溯到上一步并尝试再进行一次延迟迭代。

这不仅更短、更清晰,而且执行的正则表达式步骤也更少。 这是最有效的方法。

注意:无需担心集合。 但如果你真的在乎,你可以让你的小组成为一个非捕获小组,?:

    /*(?:[^*]**)*?/

要匹配多行注释,您需要像这样的简单正则表达式:

Regex regex = new Regex(@"/*.*?*/", RegexOptions.Singleline);

希望这对您的追求有所帮助。

问题根本不是正则表达式。这不起作用的原因是,在任何时候都只有一行代码被传递到 GetClassificationSpans() 函数中。我遇到了与您相同的问题,从您提供的代码的外观来看,我们遵循了相同的教程。

这不是一个真正的答案,它只会帮助您确定实际问题是什么。

最新更新