Skip to Content
0

Need help in Regular expressions

Nov 22, 2016 at 08:52 PM

76

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Andreas Gautsch Nov 22, 2016 at 10:11 PM
2

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

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Nov 23, 2016 at 10:17 AM
2

You can play with program DEMO_REGEX_TOY.

Share
10 |10000 characters needed characters left 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.
Show 1 Share
10 |10000 characters needed characters left 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' ).
0
Raghu Govindarajan Nov 22, 2016 at 09:47 PM
0
[[: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.

Share
10 |10000 characters needed characters left characters exceeded