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

Write REGEX for a string pattern

Former Member
0 Likes
2,903

Hi All,

I am new to using regular expression and writing the patterns, I am looking to write a pattern for below formats and do a FIND REGEX:

A123456-01-123456789-123   and A123456-01-123456789-ABC

DATA: regex TYPE REF TO cl_abap_regex,

      res   TYPE        match_result_tab,

      text  TYPE        string.

CREATE OBJECT regex

    EXPORTING pattern      = '\(.\)\1'

              simple_regex = 'X'

FIND ALL OCCURRENCES OF REGEX regex IN text RESULTS res.


could you help me write the pattern to check if user has entered string in formats A123456-01-123456789-123   and A123456-01-123456789-ABC



Thank you

Depp

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,078

Try out this snippet.

DATA str TYPE string VALUE 'A123456-01-123456789-123'.

FIND REGEX '^\w\d{6}-\d{2}-\d{9}-.{3}$' IN str.

WRITE / sy-subrc.

str = 'A123456-01-123456789-ABC'.

FIND REGEX '^\w\d{6}-\d{2}-\d{9}-.{3}$' IN str.

WRITE / sy-subrc.

/.

5 REPLIES 5
Read only

Former Member
0 Likes
1,079

Try out this snippet.

DATA str TYPE string VALUE 'A123456-01-123456789-123'.

FIND REGEX '^\w\d{6}-\d{2}-\d{9}-.{3}$' IN str.

WRITE / sy-subrc.

str = 'A123456-01-123456789-ABC'.

FIND REGEX '^\w\d{6}-\d{2}-\d{9}-.{3}$' IN str.

WRITE / sy-subrc.

/.

Read only

0 Likes
1,078

It appears you wanted separate regex patterns that match the given strings.

If that is the case, '^\w\d{6}-\d{2}-\d{9}-\d{3}$' would be for the one ending with 123, and '^\w\d{6}-\d{2}-\d{9}-\w{3}$' would be for the one ending with ABC.

Read only

0 Likes
1,078

Hi Manish,

First of all sorry for getting back so late..was stuck with project delivery...and Thank you for your solution. Only one scenario failed i.e., I didn't mention that the value 01 in the examples, is constant in strings A123456-01-123456789-123   and A123456-01-123456789-ABC.

which means it has to be always 01 any other value than 01 the sy-subrc should not be zero.

I will also be thankful if you could share me the document(s) which help us write the above patterns( '^\w\d{6}-\d{2}-\d{9}-\d{3}$' ; '^\w\d{6}-\d{2}-\d{9}-\w{3}$' ).


In the meanwhile I will also do some R&D with your reply to solve the failed scenario.

Regards,

Depp

Read only

0 Likes
1,078

Use this snippet then.

'^\w\d{6}-01-\d{9}-.{3}$'


To learn this, you can read F1 help on REGEX keyword, play around with program DEMO_REGEX_TOY and search internet for regular expression examples.

Read only

0 Likes
1,078

I'll test and come back ..:)