‎2013 Jul 08 3:35 PM
Hi ABAPers,
Need your help in a regrex.
i have a line in a workarea which contains data like USERID/0XSABUYER,USERNAME/0TXTSH,SUPERVISORID/0XARDUMMY
i dont need the technical names next to the description.
it should look like USERID,USERNAME,SUPERVISORID
I wrote a regular expression
REPLACE ALL OCCURRENCES OF REGEX '/0*,' IN ls_data-P_DATA WITH ','.
this regrex is not working for me. some how the * is not working as expected to replace all after /0 with a ,
thanks for all your help in advance.
Regards,
Bidisha Tripathi
‎2013 Jul 08 4:11 PM
Just tried a bit with program DEMO_REGEX_TOY, this seems to work:
REPLACE ALL OCCURRENCES OF REGEX '/0\w*' IN ls_data-P_DATA WITH ''.
Thomas
‎2013 Jul 08 4:11 PM
Just tried a bit with program DEMO_REGEX_TOY, this seems to work:
REPLACE ALL OCCURRENCES OF REGEX '/0\w*' IN ls_data-P_DATA WITH ''.
Thomas
‎2013 Jul 08 6:09 PM
Your solution for regex is simple as, you trying to match the characters (\u* or \w*) after the starting pattern (/0). This would fail when there is no value after /0
Like:
USERID/0,USERNAME/0TXTSH,SUPERVISORID/0XARDUMMYSo, I wanted to use the start and stop pattern to find the string but it didn't work out. Start at [/0], stop at [\,] and select all in-between them using (.*). The final regex would be
REPLACE ALL OCCURRENCES OF REGEX '[/0](.*)[\,]' IN ls_data-P_DATA WITH ''.
This doesn't work as I expected.
What it does it, selects from First Occurrence of /0 to last occurrence of ,
USERID/0,USERNAME/0TXTSH,SUPERVISORID/0XARDUMMY
That is because it is Greedy search. To make it non-greedy, I would need to use [/0](.?*)[\,]. This doesn't work either, at it gives invalid regex error. ABAP Regex integration is not yet mature!!!!
What do you think?
Thanks,
Naimesh Patel
‎2013 Jul 08 8:10 PM
I call myself a beginner when it comes to regex logic, so I cannot really argue with you. I will try around a bit more tomorrow if I find the time.
It's definitely powerful stuff that should be used much more often to solve typical problems in one or two lines rather than one or more pages of "classic" code.
I have a feeling though that just "/0" does not need to be covered by the logic in this certain case here, but only Bidisha knows and will hopefully tell us.
Thomas
‎2013 Jul 08 8:35 PM
Hi Naimesh,
I am not sure why you say that the Regex with /0\w* or /\w* or /\w{1,} would fail if there is no value after \0. I can see all the above three work even if there are no value after /0.
May be It would fail only if there is no /0 but its unlikely as it is the delimiter as per Bidisha's requirement.
Anyway, Happy REGEXing
Regards,
Karthik
‎2013 Jul 09 7:14 AM
‎2013 Jul 08 5:15 PM
Hi Bidisha,
The following Regex should also work for your requirement. After all, there are many ways a substring can be searched in REGEX.
REPLACE ALL OCCURRENCES OF REGEX '/\w*' IN ls_data-P_DATA WITH ''.
OR
REPLACE ALL OCCURRENCES OF REGEX '/\w{1,}' IN ls_data-P_DATA WITH ''.
Just a curiosity drove me to try this eventhough yours is a straight forward & short REGEX Search string . I always love to play with this REGEX thing
Regards,
Karthik
‎2013 Jul 09 7:14 AM
Hi Bidisha,
Try this code, it will work for all your requirements.
REPLACE ALL OCCURRENCES OF REGEX '[/0][\w]*' IN ls_data-p_data WITH '' IGNORING CASE.
This will replace all the values that are found after /0.
‎2013 Jul 16 8:43 AM
Hi Bidisha,
the problem that you're facing in splitting up the line you provided is causes by the fact that the current ABAP RegEx implementation only provides so called greedy matching (sometimes also called maximal matching) which means that if your regular expression contains e.g. a comma the last comma in the examined string will be found instead of the nearest one (which you would need since your lines are apparently comma-delimited).
The pattern to perform minimal matching would be *? or +? where the question mark turns on non-greedy/minimal matching. As it has already been stated in this thread this is not supported by ABAP for the time being.
There is however an - admittedly tricky - solution to your prolem by utilizing ABAP's built in JavaScript 1.5 engine. JavaScript provides the features that ABAP currently lacks, so that you can overcome this shortcoming.
Here is a mininmal vs. maximal matching example where the maximal matching uses plain ABAP and the minimal matching uses ABAP/JavaScript.
Hope that helps.
Chris
CONSTANTS:
gc_probe TYPE string VALUE 'ABCDEFZZZ'. " string to be examined
DATA:
go_regular_expression_matcher TYPE REF TO cl_abap_matcher,
gt_matches TYPE match_result_tab,
gs_match TYPE match_result,
gt_submatches TYPE submatch_result_tab,
gs_submatch TYPE submatch_result,
go_js_processor TYPE REF TO cl_java_script,
gv_pattern TYPE string,
gv_parameter TYPE string,
gv_source TYPE string,
gv_result TYPE string.
START-OF-SELECTION.
" ABAP solution - only greedy matching supported
go_regular_expression_matcher = cl_abap_matcher=>create( pattern = 'A(.*)Z'
ignore_case = abap_true
text = gc_probe ).
gt_matches = go_regular_expression_matcher->find_all( ).
LOOP AT gt_matches INTO gs_match.
gt_submatches = gs_match-submatches.
LOOP AT gt_submatches INTO gs_submatch.
gv_result = gc_probe+gs_submatch-offset(gs_submatch-length).
WRITE: / 'ABAP :', gv_result.
ENDLOOP.
ENDLOOP.
" JavaScript solution
go_js_processor = cl_java_script=>create( ).
gv_pattern = 'var reg_ex = /A(.*?)Z/i;'.
gv_parameter = 'var input = "' && gc_probe && '";'.
CONCATENATE
gv_pattern
gv_parameter
'var match = reg_ex.exec(input);'
'match[1];'
INTO gv_source SEPARATED BY cl_abap_char_utilities=>cr_lf.
gv_result = go_js_processor->evaluate( gv_source ).
WRITE: / 'JavaScript:', gv_result.