[問題] 如何取得()內不定長度的字串

看板Perl作者 (迷上西方天使)時間15年前 (2010/05/01 16:59), 編輯推噓0(006)
留言6則, 2人參與, 最新討論串1/2 (看更多)
大家好 小弟最近在練習ㄧ個題目 將.lib裡面的cell 取出來 並且依照cell名稱另存新檔 目前,已經可以成功的取出來 但是名稱上面依舊有著(),我只想保留裡面的名稱 例如 cell (FADDS1), 我只想要取出FADDS1而不要有() 但是每個名稱不見得ㄧ樣長度 不知道各位大大,有沒有好的想法可以建議ㄧ下 謝謝 open(open_file, "12.txt") or "error file!!"; my $Out; my $name; while (<open_file>) { if ($_=~ m/^cell/g) { if ($_=~ /\s(.*\b.)/) { $name=$1; open(write_file, ">$name.txt") or "error file!!"; } print $_; print write_file $_; $Skip= 1; next; } if ($Skip==1) { $Out .=$_; print $_; print write_file $_; } if ($_=~ m/^}/g) { $Skip=0; next; close write_file; } } close open_file;my $Skip; ######---------12.txt---------###### library(std_ff) { /*general attributes */ delay_molel : table_lookup; in_palce_swap_mode : match_footprint; cell (DFADDS1) { cell_footprint : addf; area : 126.7200; pin(A) { direction : input; capacitance : 0.00975; } pin(B) { direction : inoutput; capacitance : 0.1975; } pin(CI) { direction : utput; capacitance : 0.1980; } } cell (DFADDS2) { cell_footprint : addf; area : 126.7200; pin(A) { direction : input; capacitance : 0.88975; } pin(B) { direction : inoutput; capacitance : 0.1900; } pin(CI) { direction : utput; capacitance : 0.1080; } } cell (DFADDS2) { cell_footprint : addf; area : 126.7200; pin(A) { direction : input; capacitance : 0.88975; } pin(B) { direction : inoutput; capacitance : 0.1900; } pin(CI) { direction : utput; capacitance : 0.1080; } } cell (DFADDS4) { cell_footprint : addf; area : 126.7200; pin(A) { direction : input; capacitance : 0.86975; } pin(B) { direction : inoutput; capacitance : 0.1902; } pin(CI) { direction : utput; capacitance : 0.0080; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.193.176.59

05/02 00:06, , 1F
把 /\S(.*\b.)/ 改成 /\S(\(.*\b.\))/ 有用嗎?
05/02 00:06, 1F

05/02 09:27, , 2F
to drm大,那個方法好像不行耶~~~
05/02 09:27, 2F

05/02 19:31, , 3F
抱歉,忘了這是 Perl 用的,應該是 /\S((.*\b.*))/
05/02 19:31, 3F

05/02 19:43, , 4F
話說把 \b 改成 \w+ 好像會更好取?
05/02 19:43, 4F

05/03 09:09, , 5F
drm大,好像還是有點怪怪的耶~
05/03 09:09, 5F

05/03 14:46, , 6F
改成 / *\((\w+)\)/i) 就ok了
05/03 14:46, 6F
文章代碼(AID): #1Bs-rhz3 (Perl)
文章代碼(AID): #1Bs-rhz3 (Perl)