【问题标题】:find and replace in eclipse "go to nth character position"在eclipse中查找和替换“转​​到第n个字符位置”
【发布时间】:2017-09-25 05:08:37
【问题描述】:

我有一些大型 c 源文件,我想将所有行尾 cmets 对齐到第 n 个字符。采取以下措施:``

//--------------------------------------------------------------------------!
// Unveil Left to Right                                                     !
//--------------------------------------------------------------------------!

static void pUnveilL(ulong frame, ulong field)
{
    Frame* thisFrame = &Frames[frame] ;                                                         // make code more readable
    Field* thisField = &Fields[Frames[frame].fields[field]] ;                       // make code more readable

函数上方的注释大约有 60-70 个字符(我们会说 70)。所以我希望行尾 cmets 从第 71 个字符开始。像这样

//--------------------------------------------------------------------------!
// Unveil Left to Right                                                     !
//--------------------------------------------------------------------------!

static void pUnveilL(ulong frame, ulong field)
{
    Frame* thisFrame = &Frames[frame] ;                                     // make code more readable
    Field* thisField = &Fields[Frames[frame].fields[field]] ;               // make code more readable

总结一下,我认为我已经对 find 进行了排序或可以管理它,但我需要一个替换参数来开始注释 // 从行中的第 n 个位置开始。可以更换零件吗?

【问题讨论】:

  • 假设您希望注释位于第 n 个字符处,并且您有一行 n+10 字符并以注释结尾。你会如何处理这种情况?
  • 计划是首先清理文件,使行不超过所需的长度。

标签: regex eclipse


【解决方案1】:

您不能使用正则表达式和简单的搜索和替换来做到这一点,因为您必须进行数学运算。

这是执行您想做的事情的算法之一:

int position = 70;
List<string> output = new List<string>();
foreach (string line in File.ReadAllLines(@"C:\Users\me\Desktop\test.txt"))
{
    // The line contains comments AND code
    if (line.Contains(@"//") && !Regex.IsMatch(line, @"^\s*//"))
    {
        var matchCollection = Regex.Matches(line, @"(?<code>.*;)\s*(?<comment>\/\/.*)");
        string code = matchCollection[0].Groups["code"].Value;
        string comment = matchCollection[0].Groups["comment"].Value;

        output.Add(code.PadRight(position) + comment);
    }
    else
    {
        output.Add(line);
    }
}

File.WriteAllLines(@"C:\Users\me\Desktop\out.txt", output);

它是用 编写的,因为它非常接近于伪代码,您可以轻松地移植它(至少我希望如此)。以下是主要的重要部分(如果您需要更多解释,请不要犹豫):

  • Regex.IsMatch(line, @"^\s*//"): 找到只是 cmets 的行
  • (?&lt;code&gt;.*;): 一个命名的捕获组
  • code.PadRight(position): PadRigth 在字符串右边添加空格直到position

鉴于此输入:

//--------------------------------------------------------------------------!
// Unveil Left to Right                                                     !
//--------------------------------------------------------------------------!

static void pUnveilL(ulong frame, ulong field)
{
    Frame* thisFrame = &Frames[frame] ;                             // make code more readable
    Field* thisField = &Fields[Frames[frame].fields[field]] ;               // make code more readable

程序输出:

//--------------------------------------------------------------------------!
// Unveil Left to Right                                                     !
//--------------------------------------------------------------------------!

static void pUnveilL(ulong frame, ulong field)
{
    Frame* thisFrame = &Frames[frame] ;                               // make code more readable
    Field* thisField = &Fields[Frames[frame].fields[field]] ;         // make code more readable

【讨论】:

  • 抱歉这周太忙了。我只为核心答案打勾。单独查找和替换的任务过于雄心勃勃。感谢您的示例和描述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 2018-04-11
  • 2018-02-06
  • 2014-08-09
  • 1970-01-01
相关资源
最近更新 更多