JavaScript RegEx Parse Command Line into Array

86 views
Skip to first unread message

Jaye

unread,
Nov 4, 2009, 3:27:25 PM11/4/09
to Regex
I need a javascript method that will parse a command line instruction
into an array of pairs. The method should utilize the javascript
String.match or other regular expression enabled methods to implement
the parse. The regex should be implemented in a very terse
instruction. The deliverable for this project is a terse javascript
method that will parse a collection of test values and produce the
appropriate array output. The test cases can be found at
http://spreadsheets.google.com/pub?key=trvoEkjsIMF1NwBJb7vyEKw&single=true&gid=0&output=html

I posted the micro project on @oDesk http://bit.ly/3TgOJX . Probably
an easy task for you guys so let me know if you can help.

eugeny....@gmail.com

unread,
Nov 5, 2009, 5:48:31 AM11/5/09
to Regex
The nut is cracked! Still it is two step check. Judge yourself if it
is terse or not :)

1. The main regex.
Most of the examples including listed below

keyword:model train locomotive
keyword:redline
sellerid:allwheels2go
keyword:truck sellerid:allwheels2go
keyword:model train locomotive minbid:1
keyword:model train locomotive minbid:1 maxprice:50
categoryid:19175 minbid:1
categoryid:19175 desc:"flower:grown"
keyword:redline sort:bidcount
keyword:chevy truck maxdistance:100 miles
sellerid:bob,john,james,jim
sellerid:bob sellerid:john sellerid:james sellerid:jim


will be covered by regex
(\w+)\:\"?([^\r\n]+?)(?=(?:\"|\x20\w+\:|[\r\n]+|\Z))

if you grep through the line (repeat the search cyclically)


2. The check.
Now we have out of order things like

model train locomotive //no command at all
"flower:grown" //the value itself contains colon

not covered by our regex.

Not to get stuck on this, you FIRST need to check if your subject
string against

^(([a-z0-9\x20]+)|\"([a-z0-9:]+)\")$

And if that matches, use backreference $1 or $2 as collected value.
The one which is not empty is what you need. That'll be the value.
Prepend that value with "keyword" or whatever your setting is.

--------------------

What i described in step2 should in fact be the first check because

"flower:grown"

on a line by itself can be matched by the main regex and therefore
"flower" will be considered as command and "grown" as value, while
this is undesired. To avoid this, don't feed the subject string to the
main regex if the check gives you positive alert.

--
Regards, Eugeny
Reply all
Reply to author
Forward
0 new messages