user1112228
user1112228

Reputation: 13

Parse for second element of a comma delimted list

As an example I have a line of text:

61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable

I want to extract the 2nd element ballanty and the fifth element $102.

Can someone guide me perhaps?

Wanting a regex for this. There will be no comma within fields. (actually doing this in uBot).

Upvotes: 1

Views: 674

Answers (4)

Bestmacros
Bestmacros

Reputation: 1867

if you are working with Ubot you do not need any regular expression - you can use the following code:

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))

Upvotes: 0

Toto
Toto

Reputation: 91518

Split is certainly the best way, but if you really need a regex:

/^[^,]*,([^,]*),[^$]*(\$[^,]*),/

group 1 will contain ballanty

group 2 will contain $102

regex explanation:

/             : 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

Upvotes: 1

Jason Gennaro
Jason Gennaro

Reputation: 34863

Do you need to use regex?

If you are using javascript, you can divide the line into an array and extract accordingly:

var a = '61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable';
var b = a.split(',');
alert(b[1]);
alert(b[4]);

Example: http://jsfiddle.net/phMtJ/

Upvotes: 1

Dan Fego
Dan Fego

Reputation: 14024

You can do this in awk. My example is split onto two lines for clarity.

echo "61024221,ballanty,2,0,$102,,34,12/22/2011 09:08,Reliable" | \
awk -F, '{ print $2, $5 }'

The -F, option sets the comma to be the field delimiter, and then the part inside the curly braces prints the second and fifth fields.

Upvotes: 2

Related Questions