The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TechNow Ep 32 | Regular Expressions - Part 2 of 2

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

expert-logo-2.png

Chuck, Dave, and Kreg are back with more regular expression help in part 2. We'll help you understand the world of regular expressions. We'll talk about modifiers, capture groups, and more.   Part 1: TechNow Ep 31 | Regular Expressions - Part 1 of 2

Originally aired Thursday November 10, 12:00PM PST  
(Be sure to change your YouTube Setting to 720HD)
 

Ask your questions below on this discussion page.

And Please Let our Expert Know how they've helped! Comment Below!

Like, Share, Mark Helpful.


Featured Experts

find_real_file.pngChuck Tomasi is a Platform Architect for ServiceNow.   He is a computer science major with over 30 years of IT experience. As a former ServiceNow customer, Chuck won the first Innovation of the Year Award at Knowledge 10. Since joining ServiceNow in 2010 as a Technical Consultant, he has done many large scale ITSM implementations and custom applications, acted as an adjunct instructor for Education Services, created and lead the Technical Best Practices program, and co-hosts the ServiceNow series "TechNow".

 

find_real_file.pngDave Slusher has been developing software for 20 years for companies such as Intel, Orbitz, Dell Secureworks and many startups lost to history. He has been with ServiceNow for two years, first in Expert Services and now as the Developer Evangelist for the developer community and portal. He earned his BS from Georgia Tech and his MS in Computer Science from the University of Louisiana - Lafayette.

 

find_real_file.pngKreg Steppe is a Senior Curriculum Developer within ServiceNow developing and supporting cloud training infrastructure. He specializes in developing integration solutions, automating repeatable processes and Cloud Management in ITOM. Kreg's prior experience includes operating his own ISP, developing web applications in PHP, network integration, managing network support, Application Development on cloud based networks, DNS and email server maintenance. He is a Linux enthusiast and enjoys Photography.


2 REPLIES 2

Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

This Thursday... We cover part 2 of Regular Expressions including some very helpful tools to help you create regular expressions and be more powerful! Let us know if you have any questions!


Chuck Tomasi
ServiceNow Employee
ServiceNow Employee

TechNow Ep 32 - Regular Expression Examples:



Exercise 5:


// Exercise 5.0: Meta characters


var me           = 'Chuck Tomasi chuck.tomasi@servicenow.com 123 Anystreet, Anytown USA 55123 123-555-1234';



// Where is the first set of 3 digits, \d is the meta character replacement for [0-9]


var patt = /[0-9]{3,5}/; // 3-5 digits


var pos = me.search(patt);



gs.print(new StringRuler(me, patt).draw(pos));



// Exercise 5.1: Meta characters


var me           = 'Chuck Tomasi chuck.tomasi@servicenow.com 123 Anystreet, Anytown USA 55123 123-555-1234';



// Where is the phone number?


var patt = /\d{3}-\d{3}-\d{4}/; // nnn-nnn-nnnn


var pos = me.search(patt);



gs.print(new StringRuler(me, patt).draw(pos));



// Exercise 5.1.1: Meta characters


var ipAddrs = ['192.168.1.51',


                      '172.21.5.250',


                      '3.4.5',


                      '10.1.1.1',


                      '54.86.168.33',


                      '180.n.n.n'


                      ];



var patt = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/; // An IP address



for (var i = 0; i < ipAddrs.length; i++) {


      if (ipAddrs[i].search(patt) == -1) {


              gs.print('INVALID IP ADDRESS: ' + ipAddrs[i]);


      }


}



Exercise 6:


// Exercise 6.0: Matching


var me           = 'Chuck Tomasi chuck.tomasi@servicenow.com 123 Anystreet, Anytown USA 55123 123-555-1234';



// Where are all the numbers?


var patt = /\d{3,5}/; // 3-5 digits



gs.print(me.match(patt));



// Why does it only find the first instead of all?



// Exercise 6.1: Global matching


var testStr = 'A big fat black flattened cat sat on a rat at a latin ferryboat landing';



// Find all words with 'at' in them


var pattern = /\w*at\w*/g;



gs.print(testStr.match(pattern));



Exercise 7:


// Exercise 7.0: Greedy vs. lazy


var testStr = '<EM>Hello</EM>';



var pattern = /<.+>/g;



gs.print(testStr.match(pattern));



// Exercise 7.1: Greedy vs. lazy


var testStr = '<EM>Hello</EM>';



var pattern = /<.+?>/g;



gs.print(testStr.match(pattern));



Sample Data for Exercise 8:


Sample text for testing:


abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ


0123456789+-.,!@#$%^&*();/|<>"'


12345 -98.7 3.141 .6180 9,000 +42


555.123.4567 +1-(800)-555-2468


// This is a JavaScript comment:


foo@demo.net bar.ba@test.co.uk


// Here's another comment


www.demo.com http://foo.co.uk/


http://demo.com/foo.html?q=bar



Exercise 8:


// Exercise 8.0: Multi-line, starts with number



var longstr = 'Sample text for testing:\n' +


                      'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\n' +


                      '0123456789+-.,!@#$%^&*();/|<>"\'\n' +


                      '12345 -98.7 3.141 .6180 9,000 +42\n' +


                      '555.123.4567 +1-(800)-555-2468\n' +


                      '// This is a JavaScript comment:\n' +


                      'foo@demo.net bar.ba@test.co.uk\n' +


                      '// Here\'s another comment\n' +


                      'www.demo.com http://foo.co.uk/\n' +


                      'http://demo.com/foo.html?q=bar\n';



// Find all lines that start with a digit... How do I get the whole line rather than 1st character?


var patt = /^\d/gm;


var arr   = longstr.match(patt);


gs.print(arr.join('\n'));



// Exercise 8.1: Multi-line, ends with colon



var longstr = 'Sample text for testing:\n' +


                      'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\n' +


                      '0123456789+-.,!@#$%^&*();/|<>"\'\n' +


                      '12345 -98.7 3.141 .6180 9,000 +42\n' +


                      '555.123.4567 +1-(800)-555-2468\n' +


                      '// This is a JavaScript comment:\n' +


                      'foo@demo.net bar.ba@test.co.uk\n' +


                      '// Here\'s another comment\n' +


                      'www.demo.com http://foo.co.uk/\n' +


                      'http://demo.com/foo.html?q=bar\n';



// Find all lines that end with a colon


var patt = /.*:$/gm;


var arr   = longstr.match(patt);


gs.print(arr.join('\n'));



// Exercise 8.2: Multi-line, does not start with //



var longstr = 'Sample text for testing:\n' +


                      'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\n' +


                      '0123456789+-.,!@#$%^&*();/|<>"\'\n' +


                      '12345 -98.7 3.141 .6180 9,000 +42\n' +


                      '555.123.4567 +1-(800)-555-2468\n' +


                      '// This is a JavaScript comment:\n' +


                      'foo@demo.net bar.ba@test.co.uk\n' +


                      '// Here\'s another comment\n' +


                      'www.demo.com http://foo.co.uk/\n' +


                      'http://demo.com/foo.html?q=bar\n';



// Find all lines do not start with //


var patt = /^[^\/]{2}.*/gm;


var arr   = longstr.match(patt);


gs.print(arr.join('\n'));



Exercise 9:


// Exercise 9.0: Change First Last to Last, First


// Sort the list then print it



var user = [


      'Chuck Tomasi',


      'Fred Luddy',


      'Kreg Steppe',


      'Dave Slusher',


      'Annie Approver',


      'Bow Ruggeri'


];



var sortUser = [];



// Look for two 'words' separated by a space


// Save the words in separate capture groups



var patt = /(\w+)\s(\w+)/;


for (var i = 0; i < user.length; i++) {


      // Now replace it with second word, first word


      // and save it for later sorting


      sortUser.push(user[i].replace(patt, "$2, $1"));


}



gs.print(sortUser.sort().join('\n'));



// Bonus - what happens if Annie Approver is Annie         Approver?