‎2020 May 07 5:10 PM
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
‎2020 May 07 5:36 PM
Based on your description, it is not totally clear how you want to look for it.
Use the Report DEMO_REGEX_TOY to test your regex.
‎2020 May 07 5:37 PM
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
‎2020 May 07 5:49 PM
Many other variants are also possible, for instance this one:
FIND REGEX '~(\w{4})~[^~]*~(\w{4})' SUBMATCHES DATA(plant1) DATA(plant2).
‎2020 May 07 6:09 PM
Hi Michael,
PFB the answers
Thanks.
Regards,
Sanjana
‎2020 May 07 6:19 PM
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:
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.
‎2020 May 07 6:24 PM
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
‎2020 May 07 6:51 PM
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.