的第二元件作爲一個例子我有一行文本:解析爲逗號delimted列表
61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable
我要提取的第二元件ballanty
和第五元件$102
。
有人可以指導我嗎?
想要一個正則表達式。領域內不會有逗號。 (實際上是在uBot中做這個)。
的第二元件作爲一個例子我有一行文本:解析爲逗號delimted列表
61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable
我要提取的第二元件ballanty
和第五元件$102
。
有人可以指導我嗎?
想要一個正則表達式。領域內不會有逗號。 (實際上是在uBot中做這個)。
拆分肯定是最好的方式,但如果你真的需要一個正則表達式:
/^[^,]*,([^,]*),[^$]*(\$[^,]*),/
組1將包含ballanty
組2將包含$102
正則表達式的解釋:
/ : regex delimiter
^ : start of line
[^,]* : any number of char that is not a comma
, : a comma
( : start group 1
[^,]* : any number of char that is not a comma
) : end group 1
, : a comma
[^$]* : any number of char that is not $
( : start group 2
\$[^,]* : $ char followed by any number of char that is not a comma
) : end group 2
, : a comma
/ : regex delimiter
您可以在awk
中這樣做。爲了清晰起見,我的例子分成兩行。
echo "61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable" | \
awk -F, '{ print $2, $5 }'
的-F,
選項設置逗號是字段分隔符,然後將大括號內的部分打印在第二和第五字段。
你需要使用正則表達式嗎?
如果您正在使用JavaScript,您可以將行成一個陣列,並相應地提取:
var a = '61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable';
var b = a.split(',');
alert(b[1]);
alert(b[4]);
哪種編程語言?而且,您可能應該使用CSV解析器。 – kennytm 2011-12-22 17:54:57
您的CSV文件在字段內是否有逗號,例如。 '「123,213」,abc,...'? – 2011-12-22 18:00:32
對不起,我想要這個正則表達式。領域內不會有逗號。 – user1112228 2011-12-22 18:08:24