搜索单词并找到它的行号LINQ

时间:2015-10-22 15:59:25

标签: vb.net linq search directoryinfo

嘿所有我找到了以下代码HERE,并且想知道如果我还可以通过LINQ查询或其他方式获取单词出现的行号,是否可行?

<div class="page-wrap">
    <div class="form">
        <h1>Join Now</h1>
        <?php 
            echo $this->Form->create(null, ['controller' => 'users', 'action' => 'addMultiple']);   
            echo $this->Form->input('1.full_name');
            echo $this->Form->input('1.username');  
            echo $this->Form->input('1.email');
            echo $this->Form->input('1.password');
            echo $this->Form->input('1.password_confirmation', array('type' => 'password'));

            if ($current_user['role'] === 1 && isset($logged_in)) {
                echo $this->Form->input('1.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
            }   
            echo $this->Form->input('2.full_name');
            echo $this->Form->input('2.username');  
            echo $this->Form->input('2.email');
            echo $this->Form->input('2.password');
            echo $this->Form->input('2.password_confirmation', array('type' => 'password'));

            if ($current_user['role'] === 1 && isset($logged_in)) {
                echo $this->Form->input('2.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
            }

            echo $this->Form->button(__('Sign Up'));
            echo $this->Form->end();
        ?>
    </div>
</div>

该代码适用于查找单词但我真的希望能够知道它在哪个行上找到。

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

传递项目索引的LINQ Select方法has a overload

Dim startFolder = "C:\Users\me\Documents\Visual Studio 2012\Projects\project"
Dim matches =
    From f In Directory.EnumerateFiles(startFolder, "*.vb", SearchOption.AllDirectories)
    From l In File.ReadLines(f).Select(Function(x, i) New With { .Line = x, .LineNo = i + 1})
    Where l.Line.Contains(word2Search)
    Select FileName = f, LineNo = l.LineNo, Line = l.Line

匹配将是具有IEnumerableFileNameLineNo属性的Line个对象。

<强>更新

要获取文件名和匹配行索引的数组,您可以执行以下操作:

Dim matches =
    From f In Directory.EnumerateFiles(startFolder, "*.vb", SearchOption.AllDirectories)
    From l In File.ReadLines(f).Select(Function(x, i) New With { .Line = x, .LineNo = i + 1})
    Where l.Line.Contains(word2Search)
    Select File = f, LineNo = l.LineNo
    Group By File Into g = Group
    Select FileName = File, LineNos = g.Select(Function(x) x.LineNo).ToArray()

这将为您提供IEnumerable具有FileNameLineNos属性的对象。

在行中查找匹配项的位置需要进行一些更改,因为Contains只返回Boolean。您可以使用Regex.Matches查找该行中匹配的位置,所以:

Dim matches =
    From f In Directory.EnumerateFiles(startFolder, "*.vb", SearchOption.AllDirectories)
    From l In File.ReadLines(f).Select(Function(x, i) New With { .Line = x, .LineNo = i + 1})
    Where l.Line.Contains(word2Search)
    Select File = f, LineNo = l.LineNo,
        MatchPositions = Regex.Matches(l.Line, Regex.Escape(word2Search)).Cast(Of Match)().Select(Function(x) x.Index)
    Group By File Into g = Group
    Select FileName = File, Matched = g.Select(Function(x) New With { x.LineNo, .Positions = x.MatchPositions.ToArray() }).ToArray()

在此之后,您最终得到IEnumerable个具有FileNameMatched属性的对象(遗憾的是,VB.NET不会被称为Matches因为它与matches变量冲突,但你可以根据自己的喜好来玩它。 Matched属性是具有LineNoPositions属性的对象数组,Positions是字符串中索引的数组(基于零,因此添加{{1} 1}}如果你喜欢,那就在那里。