使用指令匹配 [英] Match using directives
问题描述
我有几个类,我需要的格式。我需要把命名空间内的使用指令。换句话说,我不得不改变:
I have several classes that I need to format. I need to place the using directives inside the namespace. In other words I have to change:
// some comment about system
using System;
using System.Collections.Generics; // needed to create lists
namespace MyNamespace
{
... code
到
namespace MyNamespace
{
// some comment about system
using System;
using System.Collections.Generics; // needed to create lists
... code
因此,在短期,我会想能够匹配:的
// some comment about system
using System;
using System.Collections.Generics; // needed to create lists
我迄今为止的工作是这样的正则表达式:(?S)。(?//.*(= \ R))(\ r \ N | ^)*使用*?;
第一组(//.*(= \ r)的?)(\ r \ N | ^)
匹配的评论。因此,如果使用有一个评论,我会想采取这一评论也。请注意,我才能有0或多个注释放在 *
在该组的结尾。由于某种原因,使用二是不匹配的,为什么?
the first group (//.*?(?=\r))(\r\n|^)
matches a comment. so if the using has a comment I will like to take that comment also. Note that I placed a *
at the end of the group in order to have 0 or more comments. for some reason the second using is not matched why?
推荐答案
如果你在哪里碰到这样的:
IF you where to have something like:
// comment goes here
using System.Text.RegularExpressions; // for regexes
using System.Text; /* comment */
using System.Collections.Generic; // som comment
/* this is a long comment
that spans multiple lines
in order to explain this using */
using System.Foo;
namespace EncloseCodeInRegion
{
正则表达式:
the regex:
(((//.*?\r\n)|( */\*[\s\S]*?\*/))?(\r\n)? *using .+?;(( *//.*(?=\r))|( */\*[\s\S]*?\*/))?)(?=[\s\S]*?namespace)
将所有的using语句相匹配。
would match the all the using statements.
这篇关于使用指令匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!