11

Enhancing Regex Toy – Part 2

 1 year ago
source link: https://blogs.sap.com/2023/03/27/enhancing-regex-toy-part-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
March 27, 2023 4 minute read

Enhancing Regex Toy – Part 2

This is the second in a series of six blogs describing how to enhance the regular expression tester known as Regex Toy, each blog describing a single enhancement to its capabilities.

Before applying the second patch

The preceding blog in this series described how to patch a copy of Regex Toy to enable it to observe trailing blanks appearing in the regular expression pattern. Execute that enhanced copy of Regex Toy and follow these steps:

  • Paste the following tongue twister into the Text block:

A skunk sat on a stump.  The skunk thunk the stump stunk and the stump thunk the skunk stunk.

  • Place a check mark into the IN TABLE check box.
  • Select the All Occurrences button.
  • Specify in the Regex slot of the Input block the value “k “ – a lower case “k” followed by a single space.
  • Press enter.
blog-image-12.png

As shown in the screen shot above, based on where our cursor is positioned when we hit enter, any trailing spaces specified in the Regex slot are observed when searching the Text block for matches, and the Matches block now shows the same six matches found by Regex Storm, as shown in the screen shot below:

blog-image-13.png

The reason for the second patch

  • Unlike Regex Storm, Regex Toy does not provide a way to see those spaces that match the regular expression pattern.

As illustrated above, Regex Storm uses alternating green and blue background colors to show matches, but Regex Toy uses a red foreground color, leaving spaces matching the pattern to appear the same way as spaces not matching it.

Applying the second patch

Using your favorite ABAP editor, edit the copy of ABAP repository object DEMO_REGEX_TOY containing the first patch and place the series of lines shown below into method display after the final line of the REPLACE ALL OCCURRENCES OF series of statements (first and last lines, shown preceding and succeeding a comment line of all hyphens, already exist in the code as lines 183 and 185, respectively):

183      '@@tgr@@' IN TABLE result_it WITH '</b></font>'.
       " ------------------------------------
       " DEMO_REGEX_TOY enhancement #2
       " Change highlighting from single foreground color to
       " alternating background colors:
       constants    : color_red      type string
                        value 'color="#FF0000"'
                    , background_color_green
                                     type string
                        value 'style=background-color:"#B6D840"'
                    , background_color_blue
                                     type string
                        value 'style=background-color:"#98CCE8"'
                    .
       data         : color_toggle   type int4
                    , replacement_color
                                     type string
                    .
       clear sy-subrc.
       while sy-subrc is initial.
         color_toggle                = 01 - color_toggle.
         if color_toggle eq 00.
           replacement_color         = background_color_blue.
         else.
           replacement_color         = background_color_green.
         endif.
         replace first occurrence of color_red in table result_it
            with replacement_color.
       endwhile.
       " ------------------------------------
184
185    CLEAR result_html.

This is the same second patch unchanged from the E-bite. You should find, as I did recently, that with a NetWeaver 7.5 system this patch causes Regex Toy to not work at all! To make it work, you need to adjust the two values for the constants named background_color_green and background_color_blue, as shown below:

For the value of constant background_color_green,

value 'style=background-color:"#B6D840"'
value 'style="background-color:#B6D840"'

For the value of constant background_color_blue,

value 'style=background-color:"#98CCE8"'
value 'style="background-color:#98CCE8"'

Notice in both cases the subtle change is that the value following the HTML “style=” tag needs to include the string ‘background-color:’ within the quotation marks.

Aside: When I began researching why the original patch from the E-bite did not work anymore in my Netweaver 7.5 release, I learned that the HTML ‘font’ tag is no longer supported in HTML5, and that the ‘style’ tag is recommended to be used instead. I suppose I will never know whether that original patch actually worked or I had mistakenly moved the location of the opening quotation mark before the E-bite went to press, but assuming it did work, I suspect that, at some point after the Netweaver 7.4 release I originally used, SAP may have upgraded its support of HTML rendering to become compliant with HTML5, and may explain why the original patch was no longer recognized as valid HTML.

After applying the second patch

Now activate the program and execute it using the same process described previously:

  • Paste the same tongue twister into the Text block.
  • Place a check mark into the IN TABLE check box.
  • Select the All Occurrences button.
  • Specify in the Regex slot of the Input block the value “k “, as had been specified with Regex Storm.
  • Press enter.
blog-image-14.png

As shown in the screen shot above, you should find that it now uses the same type of alternating green and blue background color to identify those portions of the Text that match the Regex pattern, as used with Regex Storm, and now those spaces matching the pattern appear highlighted.

What’s next?

Now repeat the process with both Regex Storm and Regex Toy, but this time change the regular expression pattern to the following 2-word string

the skunk

and indicate with both testers to ignore case.

With Regex Storm you should see this:

blog-image-15.png

With Regex Toy you should see this:

blog-image-16.png

As shown in the screen shot above, you should find that Regex Toy only finds the first of the two strings found by Regex Storm.

Whereas the second patch provides an improvement to Regex Toy by enabling spaces matching the pattern to be identified, it fails to find the string “the skunk” where it implicitly breaks across two lines in the Text block. This issue is addressed in the next blog in this series, Enhancing Regex Toy – Part 3.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK