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

ABAP Regular expressions

Former Member
0 Likes
842

Hello ABAPers ,

I have to restrict my string to the char's mentioned below :

A to Z

Ä Ö Ü

a to z

ä ö ü

ß 0 to 9

! " § % & / ( ) = ' + * , ; . : -

Am using the following code to do tht but it doesn't work . Would appreciate any help.

gv_string = 'ab1@'.
gv_pattern = '[A-Za-z0-9!"§%&/()=+*,;.:-ß]*'.

TRY.
    CREATE OBJECT gx_regex
      EXPORTING
        pattern = gv_pattern.
  CATCH cx_sy_regex .
ENDTRY.


TRY.
    CREATE OBJECT gx_matcher
      EXPORTING
        regex = gx_regex
        text  = gv_string.
  CATCH cx_sy_matcher .
ENDTRY.
*gt_match_result2 = gx_matcher->find_all( ).

match = gx_matcher->match( ).

Thanks ,

Sri

Edited by: Matt on Oct 25, 2010 4:27 PM - added tags

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
634

Hello Srivijaya,

You should modify your pattern : since you want any character in the list provided, you should encapsulate it into brackets :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]'

But since you have more than one character, you should repeat it n times with a star (*) after it :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'

Last thing, '-' is a special character which means for an interval, so you have to escape it with a basckslash :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'

And in order to test this easily, you can directly use it like this :

DATA : gv_string  TYPE string,
       gv_pattern TYPE string,
       match      TYPE flag.

gv_string  = 'ab1@'.
gv_pattern = '[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'.

TRY.
    CALL METHOD cl_abap_matcher=>matches
      EXPORTING
        pattern = gv_pattern
        text    = gv_string
      RECEIVING
        success = match.
  CATCH cx_sy_regex.
    WRITE : 'Unable to evaluate pattern.'.
    EXIT.
ENDTRY.

Best regards,

Samuel

Hello ABAPers ,

I have to restrict my string to the char's mentioned below :

A to Z

Ä Ö Ü

a to z

ä ö ü

ß 0 to 9

! " § % & / ( ) = ' + * , ; . : -

Am using the following code to do tht but it doesn't work . Would appreciate any help.

gv_string = 'ab1@'.
gv_pattern = '[A-Za-z0-9!"§%&/()=+*,;.:-ß]*'.

TRY.
    CREATE OBJECT gx_regex
      EXPORTING
        pattern = gv_pattern.
  CATCH cx_sy_regex .
ENDTRY.


TRY.
    CREATE OBJECT gx_matcher
      EXPORTING
        regex = gx_regex
        text  = gv_string.
  CATCH cx_sy_matcher .
ENDTRY.
*gt_match_result2 = gx_matcher->find_all( ).

match = gx_matcher->match( ).

Thanks ,

Sri

Edited by: Matt on Oct 25, 2010 4:27 PM - added tags

2 REPLIES 2
Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
634

Take a globalvariable and assignit all values and in if clause use CP to implement the same.

Nabheet

Read only

Former Member
0 Likes
635

Hello Srivijaya,

You should modify your pattern : since you want any character in the list provided, you should encapsulate it into brackets :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]'

But since you have more than one character, you should repeat it n times with a star (*) after it :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'

Last thing, '-' is a special character which means for an interval, so you have to escape it with a basckslash :

'[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'

And in order to test this easily, you can directly use it like this :

DATA : gv_string  TYPE string,
       gv_pattern TYPE string,
       match      TYPE flag.

gv_string  = 'ab1@'.
gv_pattern = '[A-Za-z0-9!"§%&/()=+*,;.:-ß*]*'.

TRY.
    CALL METHOD cl_abap_matcher=>matches
      EXPORTING
        pattern = gv_pattern
        text    = gv_string
      RECEIVING
        success = match.
  CATCH cx_sy_regex.
    WRITE : 'Unable to evaluate pattern.'.
    EXIT.
ENDTRY.

Best regards,

Samuel