1

フォーマットする必要のあるクラスがいくつかあります。名前空間内にusingディレクティブを配置する必要があります。言い換えれば、私は変更する必要があります:

// 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|^)*using .*?;

最初のグループ(//.*?(?=\r))(\r\n|^)はコメントと一致します。ですので、ご利用にコメントがございましたら、そのコメントもお受けしたいと思います。*コメントを0個以上にするために、グループの最後にを配置したことに注意してください。どういうわけか、2番目の使用法が一致しないのはなぜですか?

4

3 に答える 3

1

正規表現を試す(?s)((//[^\n\r]*)\s*?)*((using [^;]+;)\s*?)+

于 2012-07-18T15:34:08.313 に答える
0

本当に正規表現が必要ですか? 単純な文字列関数を使用しないのはなぜですか? の前にコメントとusingディレクティブを見つけてnamespace、その後ろに移動するだけです(おそらくインデントします)。

何かのようなもの:

  • ファイルを 1 行ずつ読み取る
  • 行が//(おそらく空白の後)で始まる場合は、それを覚えておいてください(コメントです)
  • 複数行のコメント ( /*to */) はよりトリッキーです。
  • 行が で始まる場合はusing、それを覚えておいてください
  • 行が で始まる場合はnamespace、それを印刷し、記憶されているすべての行を印刷してから、残りを印刷します。
于 2012-07-18T15:27:15.813 に答える
0

あなたがどこに次のようなものを持っている場合:

// 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
{

正規表現:

(((//.*?\r\n)|( */\*[\s\S]*?\*/))?(\r\n)? *using .+?;(( *//.*(?=\r))|( */\*[\s\S]*?\*/))?)(?=[\s\S]*?namespace)

すべての using ステートメントに一致します。

于 2012-07-18T15:43:40.043 に答える