Skip to Content

Help with Regex please

Hi,

I'm very new to Regex, in fact I'm very new to ABAP altogether :)

I'm trying to figure out why I have a difference in my result when I use this piece of code vs when I use the Regex Toy.

What I'm trying to do is replace the digits after the last "."

My Regex is [^.]*$ for the text 'DV-102.1.1' replace with 2

Regex toy gives me the correct answer.... DV-102.1.2

with this snippet of ABAP code however it doesn't... it gives me DV-2

REPORT znw_regex_play.

        DATA lv_count TYPE i value 1.
        DATA(lv_new_wbs_no) = lv_count + 1.
        DATA(lv_val) = 'DV-102.1.1'.
        
SPLIT lv_val AT match( val   = lv_val
                       regex = '[^.]*$' ) INTO DATA(lv_wbs_part1) DATA(lv_wbs_part2).
     
lv_val   = lv_wbs_part1 && lv_new_wbs_no .

        write: lv_val.

Can anyone tell me where I've gone wrong?

thanks.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Aug 19, 2017 at 08:57 AM

    DEMO_REGEX_TOY (find regex) and match, work identically. With regex [^.]*$ applied to 'DV-102.1.1' they both return "1".

    Your issue is only with the SPLIT, because SPLIT 'DV-102.1.1' AT '1' INTO part1 part2 gives the 2 segments 'DV-' and '02.1.1'.

    There are many ways to do what you want. I would opt for

    REPLACE REGEX '[^.]*$' IN lv_val WITH lv_new_wbs_no.

    or

    lv_val = replace( val = lv_val regex = '[^.]*$' with = lv_new_wbs_no ).
    Add comment
    10|10000 characters needed characters exceeded

    • "That would make no sense."

      A lot of things in ABAP make no sense :-)

      I can't remember the reasons why, but nevertheless that's how I understood ABAP regex to work since I first used it.

      Sometimes things don't make sense and the easiest is to shrug shoulders, accept the way it is and move on. bool vs xsdbool comes to mind...

  • Aug 18, 2017 at 04:23 PM

    Two problems:

    The "." means match a single character. So to find a real "." you have to escape it using the backslash:

    [^\.]*$

    Secondly, I think your code won't replace the last one. It looks like it will find the last "1" using the regex and then match the first "1" on the replace portion.

    A straight regex alternative is to do two matches up to and after the last "." and increment the second match by 1:

    mystr = `1.1.1.1.1`. 
    mystr = |{ match( val = mystr regex = `.*\.` }| &&
            |{ conv i( match( val = mystr regex = `[^\.]+$` ) + 1 }|.
    "Result: 1.1.1.1.2
    Add comment
    10|10000 characters needed characters exceeded

    • It's a good habit to always escape special characters, but for value sets (inside [...]) only the following characters \ [ ] (as far as I know) are considered to be special characters and need to be escaped.

  • avatar image
    Former Member
    Aug 21, 2017 at 05:30 PM
    -3

    REGEX is used to validate the specific format telephone format, zipcode format. but not replacing a value.

    for your requirement use SPLIT into table at ".", so the data after last dot comes into last record of the table. increment it by 1 again concatenate the same set of records from table separated by dot.

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 29, 2017 at 12:27 PM

    wow this generated a lot of discussion which is great!

    I debugged what regex toy was doing and opted for the REPLACE solution instead of the SPLIT.

    In the end my code is pretty much identical to what Sandra suggested.

    thanks everyone for the input. very much appreciated.

    Add comment
    10|10000 characters needed characters exceeded