2021年6月19日土曜日

deno infoをconsole.logで表示した場合とテキストファイルに出力した場合の違い

出力内容に対してRegExpを使用したら謎の結果になって、タイトルの件について確認したら違いがあることが分かりました。



console.logで表示


const 対象ファイル = Deno.args[0]
const p = Deno.run({cmd:['deno', 'info', 対象ファイル], stdout:'piped'})
const uint = await p.output()
const decoder = new TextDecoder("utf-8")
const str = decoder.decode(uint)
console.log(str)
view raw console.ts hosted with ❤ by GitHub
deno run --allow-run --allow-net console.log
view raw console hosted with ❤ by GitHub



URLだけを取得したいのにうまくマッチしない


console.logで表示した結果が上記の通りなので /[\w\d\.@\/]+/ でマッチすると考えて以下のようにしてみました。
const 対象ファイル = Deno.args[0]
const p = Deno.run({cmd:['deno', 'info', 対象ファイル], stdout:'piped'})
const uint = await p.output()
const decoder = new TextDecoder("utf-8")
const str = decoder.decode(uint)
const arr = str.split('\n')
const re = /[\w\d\.@\/]+/
for(const 行 of arr){
console.log(行 + ' > ' + 行.match(re))
}
view raw regexp.ts hosted with ❤ by GitHub
しかし実行結果は以下の通りになりました。
どこにも「0m」なんて表示されていないのに…と思って正規表現の内容を変えながら何度か試してみましたが、どうしてもイメージ通りの結果になりませんでした。



テキストファイルとして出力


deno info ts2js.ts > stdout.txt
view raw stdout hosted with ❤ by GitHub
local: C:\deno\1.11.0\ts2js.ts
type: TypeScript
dependencies: 11 unique (total 65.9KB)
file:///C:/deno/1.11.0/ts2js.ts (1.04KB)
└─┬ https://deno.land/std@0.97.0/path/mod.ts (725B)
 ├── https://deno.land/std@0.97.0/_util/os.ts (456B)
 ├── https://deno.land/std@0.97.0/path/_interface.ts (728B)
 ├─┬ https://deno.land/std@0.97.0/path/common.ts (1.14KB)
 │ └─┬ https://deno.land/std@0.97.0/path/separator.ts (259B)
 │ └── https://deno.land/std@0.97.0/_util/os.ts *
 ├─┬ https://deno.land/std@0.97.0/path/glob.ts (12.28KB)
 │ ├── https://deno.land/std@0.97.0/_util/os.ts *
 │ ├─┬ https://deno.land/std@0.97.0/path/posix.ts (14.28KB)
 │ │ ├── https://deno.land/std@0.97.0/path/_constants.ts (1.9KB)
 │ │ ├── https://deno.land/std@0.97.0/path/_interface.ts *
 │ │ └─┬ https://deno.land/std@0.97.0/path/_util.ts (3.62KB)
 │ │ ├── https://deno.land/std@0.97.0/path/_constants.ts *
 │ │ └── https://deno.land/std@0.97.0/path/_interface.ts *
 │ ├── https://deno.land/std@0.97.0/path/separator.ts *
 │ └─┬ https://deno.land/std@0.97.0/path/win32.ts (29.13KB)
 │ ├── https://deno.land/std@0.97.0/_util/assert.ts (405B)
 │ ├── https://deno.land/std@0.97.0/path/_constants.ts *
 │ ├── https://deno.land/std@0.97.0/path/_interface.ts *
 │ └── https://deno.land/std@0.97.0/path/_util.ts *
 ├── https://deno.land/std@0.97.0/path/posix.ts *
 ├── https://deno.land/std@0.97.0/path/separator.ts *
 └── https://deno.land/std@0.97.0/path/win32.ts *
view raw stdout.txt hosted with ❤ by GitHub



なぜRegExpがうまくマッチしなかったのかは分かりましたが、テキストファイルとして出力した場合のみ可視化される文字の意味は分かりませんでした…。

0 件のコメント:

コメントを投稿