2

この単純なサンプル コードを使用して、groovy でファイルを読み取っています。

file.eachLine {line->
 // do something with line
}

たとえば、私のファイルには次のようなデータがあります

blah blah blah 
This is some more lines
more lines
Insert into something
(x1,x2,x3)
(Select * from
some table
where 
something = something)
on rowid = something;

だから私はスニペットを読みたいです。最後に「セミコロン」もあるrowidの行が表示された場合。次に、「(select」まで読み返したい

したがって、このファイルを読んだ後、次を含む文字列が必要です。

(Select * from
    some table
    where 
    something = something)
    on rowid = something;

それは可能ですか?そしてどうやって?

4

2 に答える 2

1

ファイルの内容が小さい場合は、ファイル全体を読み取り、正規表現を使用して必要な部分を取得するのは簡単です。

def file = new File('/home/bart/Temp/test.txt')
def contents = file.getText()
def matcher = contents =~ /\(Select[^)]++\)\s++.*?rowid\s=\s.*;/
matcher.each { println it }

プロデュース:

(Select * from
some table
where 
something = something)
on rowid = something;
于 2009-09-29T12:31:05.833 に答える
0

リスト内の行を収集し、「;」に気付いたら、例外をスローして暗黙のループを停止します。

求める結果は、リストlist.lastIndexOf('(select')の最後にあるサブリストです。

于 2009-09-29T12:32:25.210 に答える