27 Replies · Latest reply on Aug 28, 2017 7:54 AM by Pranav Bhagat

    Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31

      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.

       

      Please Like and Share!  Find More Events on the Community!

       


      Featured Experts

       

      Chuck 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”.

       

      Dave 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.

       

       

      Kreg 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.

        • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
          Vladimir Markovic

          Looking forward for the new learning adventure.

          I used this a few times when I needed to restrict fields to accept numbers only. Being able to search for regex solution on community would save a lot of time

          So far I used following link as reference:

          Regular Expression Library

          1 of 1 people found this helpful

          Thanks,
          Vlady

          • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
            Chuck Tomasi

            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!

            --Chuck Tomasi
            --Sr. Technical Product Marketing Mgr, Now Platform
            --Join me on the Community live stream (most) weekdays at 8:00AM ET
            --https://youtube.com/user/servicenowcommunity

              • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                Chuck Tomasi

                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
                --Sr. Technical Product Marketing Mgr, Now Platform
                --Join me on the Community live stream (most) weekdays at 8:00AM ET
                --https://youtube.com/user/servicenowcommunity

              • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                Chuck Tomasi

                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));
                
                2 of 2 people found this helpful

                --Chuck Tomasi
                --Sr. Technical Product Marketing Mgr, Now Platform
                --Join me on the Community live stream (most) weekdays at 8:00AM ET
                --https://youtube.com/user/servicenowcommunity

                • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                  John Chun

                  Thanks for the great video! I had to watch it to find out what StringRuler() was.

                   

                  If any of you would like to know what the below regex does and how it works, please see my blog GQL (Glide Query Language) Part 3: The Parser

                   

                  var regex = /SELECT\s*([^]+?)\s*FROM\s*(.+?)\s*(?:WHERE\s*([^]+?(?=\s*LIMIT)|[^]+?)\s*)?(?:LIMIT\s*([,\d\s]+?)\s*)?$/
                  

                   

                  This uses many topics covered in this video, plus some.

                   

                  Also, another regex tool I like, which is not mentioned here, is

                   

                  HiFi Regex Tester - Live JavaScript Regular Expression Tester

                   

                  It highlights matches in place, which I find useful.

                   

                  Please feel free to connect, follow, mark helpful / answer, like, endorse.

                  John Chun, PhD PMP see John's LinkedIn profile

                  visit snowaid

                  • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                    Nicole Wang

                    Thanks for the video. It is really helpful.

                     

                    Can I ask you a question about regular expression for condition type field?

                     

                    I am working on a project, we need to ship data. So I have to write regex as a condition filed in a table instead in Script Include. But I find when we create a pattern string for the condition field in the platform, it will have syntax error.

                     

                    If I implement it in script include like var pattern = '/[A-Z]/'; It could work well. However we need to remove quotation and slash when we enter regular expression for condition field in platform.  And I still can not use falg i such as (?i) or '/[A-Z]/i' to make the match insensitive. Do you meet the same question? And could you give me some suggestion for this implementation? Thank you very much.

                    • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                      Pranav Bhagat

                      Hi ctomasi

                      how did u build that ui macro

                      (out of topic but want to know)

                       

                      for auto populating the codes

                      • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                        Pranav Bhagat

                        Can Anyone help

                         

                        how did u build that ui macro

                        (out of topic but want to know)

                         

                        for auto populating the codes

                        • Re: Ask the Expert: Regular Expressions Part 1 - TechNow Ep 31
                          Pranav Bhagat

                          Hi Chuck

                           

                          I tried

                          Script macro maintenance

                           

                          But its not getting invoked in scripts background