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

RegEx like in SQL WHERE

Former Member
0 Likes
1,498

Hello!

I want to search in internal tables lines like I do this in where expression of sql, but thought the instrumentality of RegEx.

For example, internal table have 2 lines:

Implementations and running times

implementation language

I want to find like in SQL such string 'implrun*'

How can I do that in RegEx?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,190

Is something like this what you're looking for?

TYPES: BEGIN OF ty_table,
 line(80) TYPE c.
TYPES: END OF ty_table.

DATA:
  l_t_table      TYPE TABLE OF ty_table,
  l_s_table      TYPE  ty_table,
  l_v_regex(80)  TYPE c VALUE 'impl(?:.+)run(?:.+)'.


l_s_table-line = 'Implementations and running times'.
APPEND l_s_table TO l_t_table.
l_s_table-line = 'implementation language'.
APPEND l_s_table TO l_t_table.


LOOP AT l_t_table INTO l_s_table.
  FIND FIRST OCCURRENCE OF REGEX l_v_regex IN l_s_table-line IGNORING CASE.
  IF sy-subrc = 0.
    WRITE:/ l_s_table-line.
  ENDIF.
ENDLOOP.

7 REPLIES 7
Read only

Former Member
0 Likes
1,191

Is something like this what you're looking for?

TYPES: BEGIN OF ty_table,
 line(80) TYPE c.
TYPES: END OF ty_table.

DATA:
  l_t_table      TYPE TABLE OF ty_table,
  l_s_table      TYPE  ty_table,
  l_v_regex(80)  TYPE c VALUE 'impl(?:.+)run(?:.+)'.


l_s_table-line = 'Implementations and running times'.
APPEND l_s_table TO l_t_table.
l_s_table-line = 'implementation language'.
APPEND l_s_table TO l_t_table.


LOOP AT l_t_table INTO l_s_table.
  FIND FIRST OCCURRENCE OF REGEX l_v_regex IN l_s_table-line IGNORING CASE.
  IF sy-subrc = 0.
    WRITE:/ l_s_table-line.
  ENDIF.
ENDLOOP.

Read only

0 Likes
1,190

Jerry Coleman, Yes, that is! Great!

I know that:

"." - any symbol

"+" - 1 or more times

But what mean "(?:.+)" ?

Read only

0 Likes
1,190

(?:.+)

There are two parts to this: The *.+* means as you expect. I put parentheses around it for readability. Since I put parentheses, this causes what's matched in the regular expression to be captured in a group.

But I don't want it captured, so I use ?: to suppress the capture. This is for performance only. You could write it as:

impl(.+)run(.+)

Read only

0 Likes
1,190

Jerry Coleman, thanks!

Read only

former_member194669
Active Contributor
0 Likes
1,190

Try this way


data: p_regex(80)  TYPE c VALUE 'impl(.+)run(.+)'.

data: regex         type ref to cl_abap_regex,
      matcher       type ref to cl_abap_matcher,
      ls_result     type match_result,
      lt_result     type match_result_tab.

condense p_regex.
    create object regex
      exporting
        pattern     = p_regex
        ignore_case = ''.

* For REGEX match
    matcher = cl_abap_matcher=>create(
                   pattern     = p_regex
                   ignore_case = ' '
                   table       = i_temp ). " i_temp is your internal table contains records

lt_result = matcher->find_all( ).

a®

Read only

0 Likes
1,190

You can try with also


impl(.*)run(.*)

a®

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,190

All details of how work regular expressions can be found in [sap library - regular expressions|http://help.sap.com/abapdocu/en/ABENREGEX_SYNTAX.htm]