Skip to Content

As mentioned in my regular expressions tutorial  (parts 1  (https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/express%20yourself%20more%20regularly.doc),[ 2 | https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/express%20yourself%20even%20more%20regularly.doc] and

3

), I’m more than happy that regular expressions – despite being  a relatively old technology – have finally been incorporated into ABAP as of Netweaver  2004s. In the last part of my tutorials I reviewed some of the tools that are  available in order to make and test regular expressions. If you are a seasoned  DIY person, you might want to do it yourself. There is a DIY show –in fact it’s  a commercial in disguise – on our regional TV where the handy man (called  Roger) always says: “What you do yourself is usually done better”. I won’t go  into the issue of whether that’s true or not though.

Pure  client<br>

            Anyway, there are several methods for testing  your regular expressions. For me, as a web developer, it’s a foregone  conclusion that it needs to be via a web application. You can make a mix of  ABAP and HTML(B), but I won’t go that far for this simple application. It can easily  be done via plain JavaScript. All you need is a form:</p>

          <p>The RE itself</p>

          <pre>Regular expression: <input type=”text” name=”regex” size=”75″ /><br></pre>

          <pre> </pre>

          <p>The string we want to test</p>

          <pre>String to test: <input type=”text” name=”teststring” size=”100″ /><br></pre>

          <pre> </pre>

          <p>The modifiers</p>

          <pre>All ocurences <input type=”checkbox” name=”global” value=”g” /> </pre>

          <pre>Case insensitive <input type=”checkbox” name=”insensitive” value=”i” /><br></pre>

          <p>And some JavaScript to test the RE.</p>

          <p>Test whether we checked some of the  modifiers</p>

          <pre>

     if (document.regextest.global.checked)

         modifier  += “g”;

     if (document.regextest.insensitive.checked)

         modifier  += “i”;</pre>

          <p>Create an RE object with the constructor</p>

          <pre>  var regex = new RegExp(document.regextest.regex.value,modifier);</pre>

          <p>Execute that RE against the string</p>

          <pre>  var matchRE = regex.exec(document.regextest.teststring.value);</pre>

          <p>As long as we find results</p>

          <pre>

     while ((matchRE =  regex.exec(document.regextest.teststring.value))!= null)

           </pre>

          <p>We add the position and the found match in  the message  </p>

          <pre>  msg += ”
at ” + matchRE.index +  ”
for “+matchRE;</pre>

          <p> </p>

          <p>Putting it all together, it comes down to  this code.</p>

          <pre><html>

   <head>

   <title>RE  tester</title>

   <SCRIPT  LANGUAGE=”JavaScript”>

             function  isMatch()

     {

     var modifier = “”;

     if (document.regextest.global.checked)

         modifier  += “g”;

     if (document.regextest.insensitive.checked)

         modifier  += “i”;

     regex = new  RegExp(document.regextest.regex.value,modifier);

     var matchRE;

     var msg = “”;

     while ((matchRE =  regex.exec(document.regextest.teststring.value))!= null)

        {

          msg  += ”
at ” + matchRE.index + ”
for “+matchRE;

          }

       if (msg != null)

            {

             alert(“Yes, it matches” + msg);

            }

      else

         {

            alert(“No, it doesn’t match”);

           }

     }

   </SCRIPT>

   </head>

   <body>

   <form  name=”regextest”>

             Regular  expression: <input type=”text” name=”regex”  size=”75″ /><br>

             String  to test: <input type=”text” name=”teststring”  size=”100″ /><br>

             All  ocurences <input type=”checkbox” name=”global”  value=”g” />

             Case  insensitive <input type=”checkbox” name=”insensitive”  value=”i” /><br></pre>

          <pre>             <input  type=”button” value=”Does it match?”  onclick=”isMatch()” />

   </form>

   </body>

   </html></pre>

          <p> </p>

          <p>The result looks like this. </p>

          <p><p>!https://weblogs.sdn.sap.com/weblogs/images/19902/re_js.jpg|height=231|alt=image|width=475|src=https://weblogs.sdn.sap.com/weblogs/images/19902/re_js.jpg|border=0!</body>

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Peter Inotai
    Hi,

    Thanks for the tutorials (and for this weblog), they are really great.

    Do you have any info about the ‘ABAP Regex Toy Demo’? It’s mentioned in OSS note 917398.
    I’ll try to patch my NSP system soon, so I’ll able to check it.

    Peter

    (0) 
      1. Peter Inotai
        Hi Eddy,

        Thanks for your feedback.

        I’ve just finished the patching (SPAM 20, ABAP SP 6-7) and found it. The program called DEMO_REGEX_TOY, it’s a nice demoprogram, similar to the ones you mentioned/showed here, but this one implemented in ABAP.

        Peter

        (0) 
    1. Eddy De Clercq Post author
      Valery,

      Thanks for the additional links.
      As mentioned in the beginning of this web log, a couple of those tools are already elaborated on in the tutorial. This web log is intended for those who want to try things out by coding it. It is also a good way to show how things are done in JS.

      Eddy

      (0) 

Leave a Reply