Skip to Content
Personal Insights
Author's profile photo Joachim Rees

Solving (or creating) problems with regular expressions (AdT or Notepad++ ) – remove unwanted newlines.

tl;dr : regexes are hard.
My half-solution: search for \R and either click “find next” or “replace (with space)”.

I can’t remember exactly (do help me out in the comments!), but someone said something along the lines of: “If you have a problem, go use regular expressions. Then you have 2 problems!”. And I think you have to start RegEx blogs with such a quote! (I didn’t make the rules! 😉 )

So I have a problem or lets call it a wish: I want nice-looking method definitions, without unneeded line-breaks and without braking my fingers from manual editing.

Fortunately, I can look forward to SAP providing something to achieve that in the future, but I want it now.

Already when writing my questions, I though “well regular expressions should be able to do that, right?” . I’ll show you how far I got, and am happy if together we can go even further.

So let’s start with a example un-clean code:

    METHODS check_for_delivery_reference
      RAISING
        zclewm_cx_input_false.
    METHODS check_is_available_stock_cat
      RAISING
        zclewm_cx_input_false.
    METHODS check_todo_final_storage
      RAISING
        zclewm_cx_input_false.
    METHODS check_for_open_wt.

    METHODS do_something_xy
      IMPORTING
        iv_param TYPE abap_bool.

Please note, this time, it’s only about the line breaks: I want to remove some of them.

The logic is: if there is a line break, that is not preceded by a “.” , remove that line break.
(note: “remove” can be also be spelled “replace with nothing (or with space)”. )

So what do we need:

\R -> any newline character.
[] -> a character list [abc] means any one charter that is a b or c.
^ -> negation the character list [^abc] means any one charter that is neither a nor b nor c.

So this is what we search for:
[^.]\R

A character, that is anything but a ., followed by a newline.

So in theory, this should work, but actually, it does not really – there are some problems!
I try to keep it short, only mentioning 2 of them I do understand:

1. It will match what it says “a non-“.” character, followed by a line break” – and AdT-search (or Notepad++) will highlight that, and make it available for replace / replace all.
But actually, that’s not what I want: I only want to replace the line break, NOT also the character preceding it.

*from this: 
    METHODS check_for_delivery_reference
      RAISING
*I want this:
    METHODS check_for_delivery_reference RAISING

*not that (the last 'e' was also replaced)
    METHODS check_for_delivery_referenc RAISING

2. I newline is also a non”.”-character, so it will match a newline followed by a newline, no matter if there is a . in front of it or not.
(3. note sure: is [cr][lf] only one or two new-line characters? )

So, as often, it is not that easy.

What I ended up doing, to save me at least the [ mouse click+and+drag to highlight, + space to remove]cycle: is this: I just search for the line break \R an the via click wither replace it with space or “find next”.

Conclusion:

While it is true that:
1. recurring manual tasks are annoying
and
2. a easy and save working automation/scripting is very rewarding

It is also true that:
0 a no-working or not-as-easy-as-thought automation is also very frustrating.
Take notice when you are going down that path and leave it as early as possible.

Do you have some regex-moments (good or bad) you want to share?

Best
Joachim

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      I really like the "similar blog post" feature here, with lead me to https://blogs.sap.com/2020/12/10/modern-regular-expressions-in-abap-part-1-introducing-pcre/ .  - might be that I want to try "Lookbehind Assertions" ....

      Author's profile photo Andrea Borgia
      Andrea Borgia

      At the uni I used to love RE to bits, but lately I haven't played with them, pity.