Application Development 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: 

Regex help

aravind_indu
Explorer
0 Kudos
797

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

bordaraju
Explorer
0 Kudos
791

Can you please try 

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

0 Kudos
744

My problem is with total length rule.

0 Kudos
743

I guess we need to rely on strlen for that ! 

Sandra_Rossi
Active Contributor
710

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?

666

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).

0 Kudos
653

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

catano
Active Participant
666

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

 

Sandra_Rossi
Active Contributor
0 Kudos
654

This regex works also with POSIX regex.

0 Kudos
653

Thanks Peter

catano
Active Participant
0 Kudos
457

Hi @aravind_indu ,

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

Best,
Peter

0 Kudos
388

Yes Peter. It is working.

324

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

catano
Active Participant
0 Kudos
294

Wonderful, I'm glad it worked.

BaerbelWinkler
Active Contributor
0 Kudos
582

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.

Vitaliy-R
Developer Advocate
Developer Advocate
0 Kudos
503

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

 

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