Regex 解析逗号删除列表的第二个元素
例如,我有一行文字:Regex 解析逗号删除列表的第二个元素,regex,Regex,例如,我有一行文字: 61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable 我想提取第二个元素ballanty和第五个元素$102 也许有人能指引我吗 想要一个正则表达式。字段中不会有逗号。(实际上是在uBot中执行此操作)。您可以在awk中执行此操作。为了清晰起见,我的示例分成两行 echo "61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable" | \ awk -F,
61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable
我想提取第二个元素ballanty
和第五个元素$102
也许有人能指引我吗
想要一个正则表达式。字段中不会有逗号。(实际上是在uBot中执行此操作)。您可以在
awk
中执行此操作。为了清晰起见,我的示例分成两行
echo "61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable" | \
awk -F, '{ print $2, $5 }'
-F,
选项将逗号设置为字段分隔符,然后大括号内的部分打印第二个和第五个字段。您可以在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]);
示例:您需要使用正则表达式吗 如果您使用的是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]);
示例:拆分当然是最好的方法,但如果您确实需要正则表达式:
/^[^,]*,([^,]*),[^$]*(\$[^,]*),/
第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
拆分当然是最好的方法,但如果您确实需要正则表达式:
/^[^,]*,([^,]*),[^$]*(\$[^,]*),/
第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
如果使用Ubot,则不需要任何正则表达式-可以使用以下代码:
set(#text,"61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable","Global")
clear list(%list from text)
add list to list(%list from text,$list from text(#text,","),"Don\'t Delete","Global")
alert($list item(%list from text,1))
alert($list item(%list from text,4))
如果使用Ubot,则不需要任何正则表达式-可以使用以下代码:
set(#text,"61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable","Global")
clear list(%list from text)
add list to list(%list from text,$list from text(#text,","),"Don\'t Delete","Global")
alert($list item(%list from text,1))
alert($list item(%list from text,4))
哪种编程语言?而且,您可能应该使用CSV解析器。您的CSV文件是否会在字段中包含逗号,例如,
“123213”、abc、
?很抱歉添加我想要的正则表达式。字段中没有逗号。哪种编程语言?而且,您可能应该使用CSV解析器。您的CSV文件是否会在字段中包含逗号,例如,“123213”、abc、
?很抱歉添加我想要的正则表达式。字段中不会有逗号。