Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Need help in Regular expressions

0 Likes
1,539

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

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

5 REPLIES 5
Read only

Former Member
0 Likes
1,403

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.
Read only

0 Likes
1,403

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' ).
Read only

raghug
Active Contributor
0 Likes
1,403
[[: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.

Read only

former_member183045
Contributor
1,403

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

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
1,403

You can play with program DEMO_REGEX_TOY.