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

Regular Expression patterns

Former Member
0 Likes
2,648

Hi,

I need to write patterns for the below examples. Can someone help me.

I need to search for regular expression for..

ZI_SD_yyyy_TOP

(The question is in the above word how can I make sure that the last of the word is 'TOP'. There should not be any more letter after TOP.

ZI_FI_DOCPOST_F01

(In the above one I need to find that the last three letters should be any alphabet followed by two numbers and there should not be any more characters after F01

Thanks,

Sri

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
2,186

To find the end of the String, you need to use the end of the string indicator $.

To find TOP at last, use TOP$

To find F01 or as a matter of fact the fist character followed by numbers, use

[A-Z][0-9][0-9]$

Something like this:

DATA: lv_input TYPE string.

DATA: result_tab TYPE match_result_tab.

DATA: mcnt TYPE i.

* positive

lv_input = 'ZI_SD_yyyy_TOP'.

FIND FIRST OCCURRENCE OF REGEX 'TOP$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

* negative

lv_input = 'ZI_SD_yyyy_TOP_1'.

FIND FIRST OCCURRENCE OF REGEX 'TOP$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

* Positive

lv_input = 'ZI_FI_DOCPOST_F01'.

FIND FIRST OCCURRENCE OF REGEX '[A-Z][0-9][0-9]$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

*negative

lv_input = 'ZI_FI_DOCPOST_F01_1'.

FIND FIRST OCCURRENCE OF REGEX '[A-Z][0-9][0-9]$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

Thanks,
Naimesh Patel

15 REPLIES 15
Read only

Former Member
0 Likes
2,186

Use this snippet.

IF 'ZI_SD_yyyy_TOP' CP 'ZI_SD*TOP'.

  WRITE:/ 'found'.

ENDIF.

IF 'ZI_FI_DOCPOST_F01' CP 'ZI_FI*F01'.

  WRITE:/ 'found'.

ENDIF.

Read only

0 Likes
2,186

So to get the F01 right:

Get the length of the string

Pos1 = length -3

Pos2 = length -2

String+Pos1(1) CN (0123456789)

String+Pos2(2) CO (0123456789)

This is pseudo code, of course.

Neal

Read only

matt
Active Contributor
0 Likes
2,186

That's not a Regular Expression.

Read only

0 Likes
2,186

Yes that was just a pattern.

Read only

naimesh_patel
Active Contributor
0 Likes
2,188

To find the end of the String, you need to use the end of the string indicator $.

To find TOP at last, use TOP$

To find F01 or as a matter of fact the fist character followed by numbers, use

[A-Z][0-9][0-9]$

Something like this:

DATA: lv_input TYPE string.

DATA: result_tab TYPE match_result_tab.

DATA: mcnt TYPE i.

* positive

lv_input = 'ZI_SD_yyyy_TOP'.

FIND FIRST OCCURRENCE OF REGEX 'TOP$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

* negative

lv_input = 'ZI_SD_yyyy_TOP_1'.

FIND FIRST OCCURRENCE OF REGEX 'TOP$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

* Positive

lv_input = 'ZI_FI_DOCPOST_F01'.

FIND FIRST OCCURRENCE OF REGEX '[A-Z][0-9][0-9]$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

*negative

lv_input = 'ZI_FI_DOCPOST_F01_1'.

FIND FIRST OCCURRENCE OF REGEX '[A-Z][0-9][0-9]$'

   IN lv_input

   MATCH COUNT  mcnt

   RESULTS result_tab.

WRITE: /(20) lv_input, (15)mcnt.

Thanks,
Naimesh Patel

Read only

matt
Active Contributor
0 Likes
2,186

Ah good. Someone who knows what a Regular Expression is.

Read only

0 Likes
2,186

Those who played with the REGEX_TOY should know what it is

Read only

0 Likes
2,186

Correct program would be DEMO_REGEX_TOY

Read only

0 Likes
2,186

Forget that, DEMO and TOY are just for Playing

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,186

Good to see you @SCN ...

RegEx has been around for quite sometime but ABAPers are still fixated on CP, NP et al. Imho they are more elegant (and sometimes even more complex ) to use than the ABAP string operators.

Honestly I tend to use simple RegEx and avoid using complex ones

Cheers,

Suhas

Read only

0 Likes
2,186

Hi Naimesh,

That was really a helpful answer, But I'm looking for more patterns here. Can we check the length of string using these regular expressions. Suppose I have "ZBH_XX'   where XX is module name say SD.

ZBH is always constant and I should only use two characters in place of XX. So totally need to check thru regular expression if the string is of length 6 and at the same time checking if ZBH_ are the first four characters and then followed by two characters only ( total length is 6 characters).

I tried using "ZBH[_][A-Z]{2} but failed to get correct entries . using FIND FIRST OCCURENCE OF regex is also showing "ZBH_ABC' also as correct answer.

I will be waiting for your answer.

Thanks,

Sri

Read only

0 Likes
2,186

Use this regex and see how value of sy-subrc changes.

FIND REGEX '^ZBH_[A-Z]{2}$' IN 'ZBH_XXABC'.

WRITE:/ sy-subrc.

FIND REGEX '^ZBH_[A-Z]{2}$' IN 'ZBH_XX'.

WRITE:/ sy-subrc.

Message was edited by: Manish Kumar

Read only

0 Likes
2,186

What you need is the $ sign at end of the regex to tell the regex to stop searching after 2 characters {2}.

So, your regex would be ZBH_[A-Z]{2}$

Use program DEMO_REGEX_TOY to test it out.

Input:

ZBH_SD

ZBH_SD_1

ZBH_ABC

ZBH_AB

Select ALL Occurnaces

Output (highlighted are match):

ZBH_SD
ZBH_SD_1
ZBH_ABC
ZBH_AB

BTW, you didn't mention this in your earlier question so don't expect to get answer. You need to ask to get the direction!

sri r wrote:

Hi Naimesh,

That was really a helpful answer, But I'm looking for more patterns here. Can we check the length of string using these regular expressions.

Thanks,

Naimesh Patel

Read only

0 Likes
2,186

Hello Suhas - Glad that you noticed Just kidding.

I try to use Regex for every possible opportunity. I need to admit, that I might not get that many chances. To your point, I know many developers don't yet realize the power of regex thus dont care to use it.

Regards,
Naimesh Patel

Read only

0 Likes
2,186

Hi Naimesh,

Thanks for the information you provided which was helpful. You are right, I should have added the second question earlier in the discussion but I realized it later. Anyways this question is now answered.