TechNow Ep 31 | Regular Expressions Part 1 of 2

Chuck Tomasi
Tera Patron

expert-logo-2.png

Chuck, Dave, and Kreg are back to help you understand the world of regular expressions. These powerful patterns can be extremely useful in scripting, Edge Encryption, and more. The team starts out with very simple examples and constructs. Find out how to understand, build, and maintain your regular expressions to take advantage of some very powerful features.

Originally aired Thursday October 13, 12:00PM PDT  
(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.


27 REPLIES 27

Chuck Tomasi
Tera Patron

Looking forward to sharing and learning more material in about an hour. Post your questions here and we'll do our best to get to them in the live broadcast!


Here's the code for the StringRuler script include if you want to make your own to run tests. Not much really. It reminds me of some of the first programs I wrote to learn looping.



var StringRuler = Class.create();


StringRuler.prototype = {


      initialize: function(inputString, inputPattern) {


            this.testStr = inputString;


            this.pattern = inputPattern;


      },



  draw : function(pos) {



            var output = '\nExpression = ' + this.pattern + '\n';



            output +=           'Position     = ' + pos + '\n';


            output += '\n';


            output += this._ruler(this.testStr.length) + '\n';


            output += this.testStr + '\n';


            output += this._spaces(pos) + '^\n';



            return output;


  },



  _ruler : function(n) {



            var line = '';



            for (var i = 0; i < n; i++) {


                      if (i % 10 == 0)


                                line += (i / 10);


                      else


                                line += '.';


            }



            return line;


  },



  _spaces : function(n) {



            var str = '';


            for (var i = 0; i < n; i++) {


                      str += ' ';


                      }



            return str;


  },



      type: 'StringRuler'


};


Chuck Tomasi
Tera Patron

TechNow Ep 31 - Regular Expression Examples:



Exercise 1:


// Exercise 1.0: Simple pattern search


var testStr = 'A big fat black cat sat on a rat';



// Find the position of the first letter 'a'


var pattern = /a/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 1.1: Two characters


var testStr = 'A big fat black cat sat on a rat';



// Find the position of the first series 'at'


var pattern = /at/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 1.2: More characters


var testStr = 'A big fat black cat sat on a rat';



// Is there a cat in the series...


var pattern = /cat/;



if (testStr.search(pattern) >= 0) {


      gs.print('Meow');


} else {


      gs.print('Try the pet store')


}



Exercise 2:


// Exercise 2.0: Brackets


var testStr = 'A big fat black cat sat on a rat';



// Find the position of the first letter 'A' or 'a'


var pattern = /[Aa]/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 2.1: Brackets


var testStr = 'A big fat black cat sat on a rat';



// Where are the letters l-p, or L-P


var pattern = /[l-pL-P]/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 2.2: Brackets


var testStr = 'My cat is 8 years old. We have had her for 7.';



// Where is the first non-character?


var pattern = /[^A-Za-z]/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 2.3: Brackets


var testStr = 'My cat is 8 years old. We have had her for 7.';



// Where is the first number?


var pattern = /[0-9]/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



// Exercise 2.4: Brackets


var testStr = 'My cat is 8 years old. We have had her for 7.';



// Where is the first odd number?


var pattern = /(1|3|5|7|9)/;


var position = testStr.search(pattern);



gs.print(new StringRuler(testStr, pattern).draw(position));



Exercise 3:


// Exercise 3.0: Declaration


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



// RegEx Pattern - option 1:


var patt1 = /[a-z]/;       // any lower case letter


var pos1 = me.search(patt1);



// RegEx Pattern - option 2:


var patt2 = new RegExp('[a-z]');


var pos2 = me.search(patt2);



gs.print(new StringRuler(me, patt1).draw(pos1));


gs.print(new StringRuler(me, patt2).draw(pos2));



Exercise 4:


// Exercise 4.0: Quantifiers


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



// Find the first set of 1 or more numbers


var patt = /[0-9]+/;


var pos = me.search(patt);



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



// Exercise 4.1: Quantifiers


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



// Find the first set of 4 numbers


// same as /[0-9][0-9][0-9][0-9]/


var patt = /[0-9]{4}/;


var pos = me.search(patt);



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



// Exercise 4.2: Quantifiers


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



// Find the first set of 3 to 5 numbers


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


var pos = me.search(patt);



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



// Exercise 4.3: Quantifiers


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



// Whether the string starts with...


var patt = /^servicenow/;


var pos = me.search(patt);



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



// Exercise 4.4: Quantifiers


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



// Whether the string ends with...


var patt = /-[0-9]{4}$/;


var pos = me.search(patt);



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



// Exercise 4.5: Quantifiers


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



// This may look redundant, but not exactly the same as /[0-9]{3}/


// Test if there is a 3 digit sequence anywhere in the string


var patt = /^.*[0-9]{3}.*$/;


var pos = me.search(patt);



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



// Exercise 4.6: Quantifiers


var string = 'How will this match?';



// Find a t followed by a c


var pat = /t(?=c)/;



var pos = string.search(pat);


gs.print(new StringRuler(string, pat).draw(pos));



// Exercise 4.7: Quantifiers


var string = 'How will this match?';



// find a t that is NOT followed by an h


var pat = /t(?!h)/;



var pos = string.search(pat);


gs.print(new StringRuler(string, pat).draw(pos));


ohhh darn..



I watched the thing through youtube.. sat and wrote down the example so I easy could find them later if needed.


After the video I went here to the community to check out the comments and I find you already wrote down all the examples...



In part 2 I'm expecting to find all the examples on the community as well



//Göran


Thanks a lot Chuck for the examples. Helped a lot, looking forward for the Part 2 !