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

Issue with regrex statement

bidisha_tripathi
Explorer
0 Likes
1,794

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

1 ACCEPTED SOLUTION
Read only

ThomasZloch
Active Contributor
0 Likes
1,376

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

8 REPLIES 8
Read only

ThomasZloch
Active Contributor
0 Likes
1,377

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

Read only

0 Likes
1,376

Hello & ,

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/0XARDUMMY

So, 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

Read only

0 Likes
1,376

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

Read only

0 Likes
1,376

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

Read only

0 Likes
1,376

Hi

The ending of pattern is just one character (comma).

We can get rid of greediness in this case by negating comma.

DATA lv_str TYPE string VALUE 'USERID/0XSABUYER,USERNAME/0TXTSH,SUPERVISORID/0XARDUMMY'.

REPLACE ALL OCCURRENCES OF REGEX '/0[^,]*' IN lv_str WITH ''.

WRITE:/ lv_str.

Read only

Former Member
0 Likes
1,376

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

Read only

Former Member
0 Likes
1,376

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.

Read only

Former Member
0 Likes
1,376

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.