我想在 C# 中的多行注释中使用正则表达式。我有@"/[*][\w\d\s]+[*]/"
,但使用该表达式仅注释出现在单行中 /* */ 之间的文本,而不是多行中出现的文本。
单线:
/* 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(@"/\*(?:(?!\*/)(?:.|[\r\n]+))*\*/");
_quoted.Add(@"([""'])(?:\\\1|.)*?\1");
_numeric.Add(@"\b\d+\b")
_keywords.Add(@"\bif\b");
_keywords.Add(@"\belse\b");
_keywords.Add(@"\bforeach\b");
_keywords.Add(@"\bswitch\b");
_keywords.Add(@"\bcase\b");
.
.
.
_identifierTypes.Add(@"\bint\b");
_identifierTypes.Add(@"\bdate\b");
_identifierTypes.Add(@"\bstring\b");
.
.
.
}
#endregion
#endregion
};
不确定这是否有帮助,但从我所看到的情况来看与您的示例非常相似。提前致谢