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

Regex help

aravind_indu
Explorer
0 Likes
4,252

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.

15 REPLIES 15
Read only

bordaraju
Explorer
0 Likes
4,246

Can you please try 

^[A-Z]{3,7}\.[A-Z]{3,7}$

Read only

0 Likes
4,199

My problem is with total length rule.

Read only

0 Likes
4,198

I guess we need to rely on strlen for that ! 

Read only

Sandra_Rossi
Active Contributor
4,165

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?

Read only

4,121

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(regexcl_abap_regex=>create_pcre(
      pattern `(\w{3,10})(?C1)(\.)(?C3)(\w{3,10})(?C2)` ).

    DATA(matcherregex->create_matchertext lv_text ).

    DATA(handlerNEW handle_regex).
    matcher->set_callouthandler ).
    DATA(lv_resultmatcher->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).

Read only

0 Likes
4,108

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

Read only

catano
Active Participant
4,121

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

 

Read only

Sandra_Rossi
Active Contributor
0 Likes
4,109

This regex works also with POSIX regex.

Read only

0 Likes
4,108

Thanks Peter

Read only

catano
Active Participant
0 Likes
3,912

Hi @aravind_indu ,

Have you managed to make it work maybe with the regexp?
Meaning that my answer is a solution? 

Best,
Peter

Read only

0 Likes
3,843

Yes Peter. It is working.

Read only

3,779

Can you mark it as "answered" then @aravind_indu ?

Read only

catano
Active Participant
0 Likes
3,749

Wonderful, I'm glad it worked.

Read only

BaerbelWinkler
SAP Champion
SAP Champion
0 Likes
4,037

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.

Read only

Vitaliy-R
Developer Advocate
Developer Advocate
0 Likes
3,958

Ensure your regex engine supports lookahead assertions (?=...) and then try

 

^(?=.{10,18}$)[A-Za-z]{3,7}\.[A-Za-z]{3,10}$