Sunday, 15 March 2015

regex - find and get particular word using regular expression -



regex - find and get particular word using regular expression -

am trying find , particular word info returnd

while executing partiular command console.log(stdout) prints below data

commit e6c4236951634c67218172d742 author:ttpn <testuser@gmail.com> date: mon jun 9 10:18:04 2014 -0700 prepare dersk reset .......... ...........

my code

var getemail = function (callback) { var command = "git show ec6c4236951634c67218172d742"; exec(command, function (error, stdout, stderr) { if (error !== null) { console.log(error) callback(error, null); } else { console.log(stdout) // prints above info } })

; }

from how can email id testuser@gmail.com using regular look possible?

this regex capture info want groups 1 , 2 (on demo, @ capture groups in right pane):

commit (\s+)[\r\n]*author:[^<]*<([^>]*)>

here how utilize in js:

var regex = /commit (\s+)[\r\n]*author:[^<]*<([^>]*)>/; var match = regex.exec(string); if (match != null) { theid = match[1]; theemail = match[2]; }

explaining regex

commit # 'commit ' ( # grouping , capture \1: \s+ # non-whitespace (all \n, \r, \t, \f, # , " ") (1 or more times (matching # amount possible)) ) # end of \1 [\r\n]* # character of: '\r' (carriage return), # '\n' (newline) (0 or more times (matching # amount possible)) author: # 'author:' [^<]* # character except: '<' (0 or more times # (matching amount possible)) < # '<' ( # grouping , capture \2: [^>]* # character except: '>' (0 or more # times (matching amount # possible)) ) # end of \2 > # '>'

regex node.js shell

No comments:

Post a Comment