2013 Jun 06 2:51 PM
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
2013 Jun 06 5:29 PM
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
2013 Jun 06 3:29 PM
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.
2013 Jun 06 3:41 PM
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
2013 Jun 06 6:38 PM
2013 Jun 07 3:12 AM
2013 Jun 06 5:29 PM
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
2013 Jun 06 6:37 PM
2013 Jun 06 9:01 PM
2013 Jun 06 9:04 PM
2013 Jun 06 9:09 PM
2013 Jun 07 7:17 AM
2013 Jun 07 2:02 PM
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
2013 Jun 07 2:24 PM
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
2013 Jun 07 2:39 PM
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
2013 Jun 07 2:46 PM
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
2013 Jun 10 7:59 AM
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.