Skip to Content

Need help in Regular expressions

Hello experts,

I need help with the regular expressions. I have tried my best but could not achieve. My requirement is to check a variable for pattern like number + alphabet i.e 12c, 150b, 2500c, 1b Last digit should always be either c or b, there should be only a number before b or c. Please help me in this regard.

Thanks,

Petru

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Nov 22, 2016 at 10:11 PM

    If you want to match e.g. 123c but not a123c then you should add the \b (word boundary) token.

    \b[0-9]+[bc]\b

    But there are a lot of more possibilities which you can best explore in one of the numerous online regex tester sides like

    https://regex101.com/ as regex expressions are not an ABAP topic only

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 23, 2016 at 10:17 AM

    You can play with program DEMO_REGEX_TOY.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 22, 2016 at 09:12 PM
    -2

    This can be done by some simple conditions as below.

    For e.g. your alphanumeric variable is p_test below logic can be used to check the condition

    DATA: lv_len TYPE i,
          lv_last TYPE i,
          p_test type char20.
    
    CONDENSE p_test.
    lv_len = STRLEN( p_test ).
    lv_last = lv_len - 1.
    
    IF p_test+0(lv_last) CO '1234567890' AND
    ( p_test+lv_last(lv_len) EQ 'B' OR p_test+lv_last(lv_len) EQ 'C' ).
    .....
    ...
    ..
    ...
    ENDIF.
    Add comment
    10|10000 characters needed characters exceeded

    • And you get a short dump - At the end what you are doing for a 15 digit pattern (example: 12345678901234c) is p_test+14(15). You will get an error CX_SY_RANGE_OUT_OF_BOUNDS. Even with this the IF statement should be...

      IF p_test+0(lv_last) CO '1234567890' AND
      ( p_test+lv_last(1) EQ 'B' OR p_test+lv_last(1) EQ 'C' ).
  • Nov 22, 2016 at 09:47 PM
    [[:digit:]]+[bc]

    or

    \d+[bc]

    or

    [0-9]+[bc]

    Both [[:digit:]] and \d mean the same thing and is essentially the same as [0-9] meaning any number between 0 and 9. The '+' means 1 or more occurrences; if you want zero or more, use '*' instead. The [bc] means either b or c.

    Add comment
    10|10000 characters needed characters exceeded