2016-05-20 2 views
1

まあまあ、私はRegExを初めて使っています。RegExはタブ区切りのテキストですべての行を処理していません

https://regex101.com/r/mS2oB5/4

私の正規表現

(\w.+?)\t(\w.+?)\t(\w.+?)\t(\d)\t(\w.+?)\t(\w.+?)\t(\w.+?)\t(\w.+?)\t(\w.+?)\t(\w.+?)\t(\w.+?)\t(\w.+) 

テキストファイル(タブ区切り)

C-380_TF-4318-028 none ID 4 none none 1.1 c 1.4 none none 26 RB 2,5% 
C-366_TF-4269-012 none REW 7 none none 1.1 c 17.1 none none 28 RB 7% 
C-1008_TF-4480-011 none none 0 WT H 1.1 c 17.16 none none 24 R 
C-1008_TF-4480-006 none none 0 WT H 1.1 c 2 none none 36 R 
C-388_TF-4351-022 none none 0 WM none 1.1 c 20.3 none none 21 R 
C-388_TF-4351-019 none none 0 WM none 1.1 c 22.3 none none 32 R 
C-397_TF-4437-001 none REW 7 WM none 1.1 c 26 none none 30 RB 7,5% 
C-388_TF-4351-013 none none 0 WM none 1.3 b 17.3 none none 14 RB 
C-366_TF-4269-004 none none 0 none none 2.1 17 none none n.d. R 
C-1008_TF-4480-013 none REW 7 WT H 2.1 c 1 none none 28 RB 
C-380_TF-4318-026 none REW 7 none none 2.1 c 1.2 none none 28 RB 2,5% 
C-380_TF-4318-016 none none 0 none none 2.1 c 17.1 none none 28 RB 2,5% 
C-380_TF-4318-015 none none 0 none none 2.1 c 6.36 none none 26 RB 10% 
C-397_TF-4437-002 none none 0 WM none 2.3 c 15.2 none none 28 RB 5% 
C-385_TF-4344-000b none ED 23 none none 2.3 c 2 10.4.3 none 26,5 CO 100% 
C-385_TF-4344-000a none REW 7 none none 2.5.1 c 20.2 none none 30 RB 21% 
C-366_TF-4269-022 none KW 7 none none 2.5.2 b 17.1 none none 10 RB 7% 

がなぜ発現は、いくつかのエントリに取り組んで、その他のものに動作していません、私は見当もつかない?ああ、助けてくれてありがとう!

+1

'.'は' \ w'と '\ t'にもマッチします。あなたの正規表現が完了するために必要なステップの数に気付いたことがありますか?タブ区切りファイルなので、タブで区切ってみましょう。さて、[この正規表現](https://regex101.com/r/pR8bJ1/1)を見てください(それぞれの行を '\ t 'で分割するだけで済むので意味がありません)。 –

+0

あなたの助けwiktorに感謝します!それは本当にトリックでした。私が言ったように、私のsully質問には申し訳ありません、私は3年前の学習beeingより多くの単純なマークアップへの最初のステップbeeing .... – utor

+0

より良いCSVパーサーを使用して - 適切な設定では、効率的かつ正確に –

答えて

0

実際の問題は、各項目の長さを2文字以上にする必要があることです。\w.+?です。 \wは1語の文字に一致し、.+?は少なくとも1文字に一致します。最後の\w.+には少なくとも2つのシンボルも必要です。また、.\w\d\tの両方にマッチすることを覚えていれば、あなたの正規表現は非常に非効率的です。

ファイルを処理する最善の方法は、行に分割し、各行をタブで分割することです。確かに、CSVパーサを強くお勧めします。オーバーヘッドを減らすためにバックトラックタブ以外の0+文字、および^/$アンカー -

ただ教育目的のために、行の各項目が[^\t]*と一致するthis regexを参照してください。

+0

あなたの助けをもう一度ありがとう、私はそれをたくさんありがとう:thumbsup! – utor

関連する問題