2024 Dec 04 2:37 AM
Hi,
I'm trying to develop a regex pattern to search a string. The string is like 'Aravind.Induchudan'.
Whatever before dot can be 3 to 7 characters and whatever after dot can be also 3 to 7 characters.
The regex pattern is '[A-Z]{3,10}\.[A-Z]{3,10}'.
The next rule the total length should be 10-18 characters. How to do this. Any help is appreciated.
2024 Dec 04 2:54 AM
2024 Dec 04 3:55 AM
2024 Dec 04 4:02 AM
2024 Dec 04 6:29 AM
What kind of regex: POSIX, PCRE or other? If you don't know, please provide your minimal ABAP version. Why can't you use STRLEN?
2024 Dec 04 8:43 AM
Using callout it is possible.
DATA lv_text TYPE string VALUE `Aravind.Induchudan is a mail id. This is great.`.
lv_text = lv_text && ` Sam.Tom is another mail id. Anirudh.Induchud is another mail`.
CLASS handle_regex DEFINITION.
PUBLIC SECTION.
DATA lv_len TYPE i.
INTERFACES if_abap_matcher_callout.
ENDCLASS.
CLASS handle_regex IMPLEMENTATION.
METHOD if_abap_matcher_callout~callout.
DATA lv_result TYPE if_abap_matcher_callout=>callout_result.
IF callout_num EQ 1.
lv_len = capture_last_len.
ELSEIF callout_num EQ 2.
lv_len = capture_last_len + lv_len.
IF lv_len LT 10 OR lv_len GT 18.
callout_result = IF_ABAP_MATCHER_CALLOUT=>c_callout_result-FAIL.
ENDIF.
ENDIF.
ENDMETHOD.
ENDCLASS.
CLASS demo_pcre DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo_pcre IMPLEMENTATION.
METHOD main.
DATA(regex) = cl_abap_regex=>create_pcre(
pattern = `(\w{3,10})(?C1)(\.)(?C3)(\w{3,10})(?C2)` ).
DATA(matcher) = regex->create_matcher( text = lv_text ).
DATA(handler) = NEW handle_regex( ).
matcher->set_callout( handler ).
DATA(lv_result) = matcher->match( ).
TRY.
CALL METHOD matcher->find_all
RECEIVING
matches = DATA(lt_result).
LOOP AT lt_result INTO DATA(ls_result).
WRITE: /, lv_text+ls_result-offset(ls_result-length).
ENDLOOP.
CATCH cx_sy_matcher.
ENDTRY.
cl_demo_output=>display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo_pcre=>main( ).
2024 Dec 04 9:28 AM - edited 2024 Dec 04 9:32 AM
Thanks, interesting to see a callout example. So, PCRE (since ABAP 7.55). I guess it's more an exercise than real life code, because testing the match length is a much shorter code:
FIND ALL OCCURRENCES OF PCRE '...' IN '...' RESULTS DATA(matches).
LOOP AT matches WHERE length BETWEEN 10 AND 18...
...
ENDLOOP.
More about PCRE/Callouts in ABAP: Modern Regular Expressions in ABAP - Part 1 - Intr... - SAP Community
2024 Dec 04 8:41 AM
Hi @aravind_indu ,
I'm not sure if lookaheads are working, but with a Perl like syntax it is look like this:
^(?=.{10,18}$)[a-zA-Z]{3,10}\.[a-zA-Z]{3,10}$
Regards,
Peter
2024 Dec 04 9:16 AM
2024 Dec 04 9:32 AM
2024 Dec 06 1:06 PM
Hi @aravind_indu ,
Have you managed to make it work maybe with the regexp?
Meaning that my answer is a solution?
Best,
Peter
2024 Dec 10 5:31 AM
2024 Dec 11 12:33 PM
2024 Dec 11 11:52 PM
2024 Dec 04 11:09 AM
Do you have access to a NetWeaver ERP system? You could run program DEMO_REGEX_TOY via SE38 there and see what happens. It also has online help with many pointers regarding REGEX.
2024 Dec 05 3:30 PM
Ensure your regex engine supports lookahead assertions (?=...) and then try
^(?=.{10,18}$)[A-Za-z]{3,7}\.[A-Za-z]{3,10}$