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 expression

sanjana_lingras
Active Participant
0 Likes
2,103

Hi Expert,

How can we make use of Regex to find specific pattern e.g. Plant value from string. This plant will be either numeric or alphanumeric and 4 digit code.

PFB the example:

TEST Plant~A030~NEW TEST Plant~0320

From above string, I want to extract 2 values i.e. A030 & 0320.

How can form the regex for this scenario?

Regards,

Sanjana

7 REPLIES 7
Read only

michael_piesche
Active Contributor
1,891

Based on your description, it is not totally clear how you want to look for it.

  • Do you just want to look for the two specific known codes, e.g. A030 and 0320?
    With the regex 'A030|0320' you look for A030 and 0320
  • Do you want to look for any kind of values that could possible be the technical code of a plant from table T001W? In that case you would have to read all plants from T001W, concatenate them into a string separated by | and use that as your regex.
  • Or do you just want to look for the next four digits after the substring "Plant~" and make sure that these four digits are either numbers and/or capital letters?

Use the Report DEMO_REGEX_TOY to test your regex.

Read only

venkateswaran_k
Active Contributor
0 Likes
1,891

Hi Sanjana

Try this

 
DATA: result_tab TYPE match_result_tab.
FIND ALL OCCURRENCES OF regex 'Plant~*' IN 'TEST Plant~A030~NEW TEST Plant~0320'
                     RESULTS result_tab.

The result tab will give you the Offset Position.

From there you can pick the Plant name

Regards,

Venkat

Read only

0 Likes
1,891

Many other variants are also possible, for instance this one:

FIND REGEX '~(\w{4})~[^~]*~(\w{4})' SUBMATCHES DATA(plant1) DATA(plant2).
Read only

sanjana_lingras
Active Participant
0 Likes
1,891

Hi Michael,

PFB the answers

  • Do you just want to look for the two specific known codes, e.g. A030 and 0320?
    With the regex 'A030|0320' you look for A030 and 0320
  • --> No specific plant , plant could be any 4 digit plant concatenated in string , either numeric or alphanumeric
  • Do you want to look for any kind of values that could possible be the technical code of a plant from table T001W? In that case you would have to read all plants from T001W, concatenate them into a string separated by | and use that as your regex.
  • --> No need of T001W plant validation.
  • Or do you just want to look for the next four digits after the substring "Plant~" and make sure that these four digits are either numbers and/or capital letters?
  • --> This format is not fixed Plant~*, it may come at any place. We just need to extract those 4 digit (numeric or alphanumeric) substring from main string.

Thanks.

Regards,

Sanjana

Read only

michael_piesche
Active Contributor
1,891

If the plant is not superceded by a specific substring (like for instance 'Plant~') and it is not validated (like against T001W), how will you know, that the found four digits are actually a plant? Based on you example "TEST Plant~A030~NEW TEST Plant~0320", if you test for consecutive four alphanumerical values and possible letters in capital only, you would get this result instead:

  • TEST
  • A030
  • TEST
  • 0320

So clearly, TEST shouldnt be a plant, but without validation and without superceding substring, it will be identfied as such by the regex.

(If you dont test against capital letters, you would also get 'Plan' and 'lant' twice.)

Please review your requirements and give a feedback.

Read only

sanjana_lingras
Active Participant
0 Likes
1,891

Yes , Micheal I agree validation for plant is needed but fetching all plants from T001W and comparing each plant with input string to check if it valid or not is bit performance intensive.

so better way would be to extract 4 digit numeric and alphanumeric code and then validate it against T001W in one go later on.

Regards,

Sanjana

Read only

Sandra_Rossi
Active Contributor
1,891

You should define a clear rule, otherwise people will come again and again saying that in some situations there is a bug, and you won't be able to protest because you can't tell the rule. Worse than that, any "correction" will then provoke regressions for some other people.