Программа пример для работы с регулярными выражениями

Выкладываю пример программы для тестирования регулярных выражений.

Ниже код скопированный со стандартной программы SAP, программа называется DEMO_REGEX_TOY

DEMO_REGEX_TOY - Работа с регулярными выражениями

  1. REPORT regex_toy.
  2.  
  3. CONSTANTS: line_len TYPE i VALUE 80,
  4. marker TYPE string VALUE `@@tgl@@$0@@tgr@@`.
  5.  
  6. TYPES: textline TYPE c LENGTH line_len.
  7.  
  8. DATA: regex TYPE c LENGTH 255,
  9. new TYPE c LENGTH 255,
  10. opfind TYPE c LENGTH 1 VALUE 'X',
  11. oprepl TYPE c LENGTH 1,
  12. opmatch TYPE c LENGTH 1,
  13. first TYPE c LENGTH 1 VALUE 'X',
  14. all TYPE c LENGTH 1,
  15. nocase TYPE c LENGTH 1 VALUE 'X',
  16. case TYPE c LENGTH 1,
  17. sub1 TYPE c LENGTH 80,
  18. sub2 TYPE c LENGTH 80,
  19. sub3 TYPE c LENGTH 80,
  20. sub4 TYPE c LENGTH 80,
  21. sub5 TYPE c LENGTH 80,
  22. sub6 TYPE c LENGTH 80,
  23. docu TYPE REF TO cl_gui_control.
  24.  
  25. CLASS demo DEFINITION.
  26. PUBLIC SECTION.
  27. CLASS-METHODS: main, init.
  28. PRIVATE SECTION.
  29. CLASS-DATA: result_area TYPE REF TO cl_gui_custom_container,
  30. result_view TYPE REF TO cl_gui_html_viewer,
  31. result_it TYPE TABLE OF string,
  32. text_area TYPE REF TO cl_gui_custom_container,
  33. text_view TYPE REF TO cl_gui_textedit,
  34. text_wa TYPE string.
  35. CLASS-METHODS display.
  36. ENDCLASS. "demo DEFINITION
  37.  
  38. CLASS demo IMPLEMENTATION.
  39. METHOD init.
  40. `Cathy's black cat, fast asleep on the mat,`
  41. `dreamt that a bat was stuck in Matt's hat.`
  42. `And being a fat but cute little cat`
  43. `she smacked the poor bat quite thoroughly flat.`
  44. INTO text_wa SEPARATED BY cl_abap_char_utilities=>cr_lf. "#EC NOTEXT
  45. regex = '.'.
  46. CLEAR new.
  47.  
  48. CREATE OBJECT: result_area EXPORTING container_name = 'RESULT',
  49. result_view EXPORTING parent = result_area,
  50. text_area EXPORTING container_name = 'TEXT',
  51. text_view EXPORTING parent = text_area
  52. wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position
  53. wordwrap_position = line_len
  54. wordwrap_to_linebreak_mode = cl_gui_textedit=>true.
  55. text_view->set_textstream( text = text_wa ).
  56. text_view->set_statusbar_mode( statusbar_mode = cl_gui_textedit=>false ).
  57. text_view->set_toolbar_mode( toolbar_mode = cl_gui_textedit=>false ).
  58. ENDMETHOD. "init
  59.  
  60. METHOD main.
  61. DATA: new_marked TYPE string,
  62. pattern TYPE string.
  63. DO. "#EC DO_OK
  64. text_view->get_textstream( IMPORTING text = text_wa ).
  65. cl_gui_cfw=>flush( ). " update text_wa from control
  66. SPLIT text_wa AT cl_abap_char_utilities=>cr_lf INTO TABLE result_it.
  67. new_marked = marker.
  68. IF oprepl = 'X'.
  69. REPLACE '$0' IN new_marked WITH new.
  70. IF opmatch = 'X'.
  71. CONCATENATE '^' regex '$' INTO pattern.
  72. pattern = regex.
  73. CLEAR: sub1, sub2, sub3, sub4, sub5, sub6.
  74.  
  75. TRY.
  76. IF nocase = 'X'.
  77. FIND REGEX pattern IN TABLE result_it IGNORING CASE
  78. SUBMATCHES sub1 sub2 sub3 sub4 sub5 sub6.
  79. IF first = 'X'.
  80. REPLACE REGEX pattern IN TABLE result_it
  81. WITH new_marked IGNORING CASE.
  82. ELSE. " all = 'X'
  83. REPLACE ALL OCCURRENCES OF REGEX pattern IN TABLE result_it
  84. WITH new_marked IGNORING CASE.
  85. ELSE. " case = 'X'
  86. FIND REGEX pattern IN TABLE result_it
  87. SUBMATCHES sub1 sub2 sub3 sub4 sub5 sub6.
  88. IF first = 'X'.
  89. REPLACE REGEX pattern IN TABLE result_it
  90. WITH new_marked.
  91. ELSE. " all = 'X'
  92. REPLACE ALL OCCURRENCES OF REGEX pattern IN TABLE result_it
  93. WITH new_marked.
  94. CATCH cx_sy_regex.
  95. MESSAGE 'Invalid regular expression' TYPE 'S' DISPLAY LIKE 'E'. "#EC NOTEXT
  96. CLEAR result_it.
  97. display( ).
  98. ENDMETHOD. "main
  99.  
  100. METHOD display.
  101. CONSTANTS linesize TYPE i VALUE 255.
  102. DATA: result_html TYPE TABLE OF w3_html,
  103. result_wa LIKE LINE OF result_it,
  104. url TYPE c LENGTH 255,
  105. n TYPE i.
  106.  
  107. REPLACE ALL OCCURRENCES OF: "Prepare result for HTML browser
  108. '&' IN TABLE result_it WITH '&',
  109. '"' IN TABLE result_it WITH '"',
  110. '<' IN TABLE result_it WITH '&lt;',
  111. '>' IN TABLE result_it WITH '&gt;',
  112. ` ` IN TABLE result_it WITH '&nbsp;',
  113. '@@tgl@@' IN TABLE result_it WITH '<font color="#FF0000"><b>',
  114. '@@tgr@@' IN TABLE result_it WITH '</b></font>'.
  115. CLEAR result_html.
  116. APPEND '<html><body><font face="Arial monospaced for SAP, Courier New" size="-1">' TO result_html.
  117. CONCATENATE LINES OF result_it INTO result_wa SEPARATED BY '<br/>'.
  118. n = strlen( result_wa ).
  119. WHILE n > linesize.
  120. APPEND result_wa(linesize) TO result_html.
  121. SHIFT result_wa LEFT BY linesize PLACES.
  122. SUBTRACT linesize FROM n.
  123. APPEND result_wa TO result_html.
  124. APPEND '</font></body></html>' TO result_html.
  125. result_view->load_data( IMPORTING assigned_url = url
  126. CHANGING data_table = result_html ).
  127. result_view->show_url( url = url ).
  128.  
  129. CALL SCREEN 100.
  130.  
  131. IF sy-ucomm = 'DOCU'.
  132. cl_abap_docu=>show(
  133. EXPORTING
  134. area = 'ABEN'
  135. name = 'REGEX_SYNTAX'
  136. IMPORTING
  137. docu_container = docu ).
  138.  
  139. ENDMETHOD. "display
  140. ENDCLASS. "demo IMPLEMENTATION
  141.  
  142. *----------------------------------------------------------------------*
  143.  
  144. demo=>init( ).
  145. demo=>main( ).
  146.  
  147. *----------------------------------------------------------------------*
  148.  
  149. MODULE status_0100 OUTPUT.
  150. SET TITLEBAR 'TITLE_100'.
  151. SET PF-STATUS 'SCREEN_100'.
  152. IF NOT docu IS INITIAL.
  153. CALL METHOD cl_gui_control=>set_focus
  154. EXPORTING
  155. control = docu.
  156. CLEAR docu.
  157. ENDMODULE. "status_0100 OUTPUT
  158. *
  159. MODULE cancel INPUT.
  160. LEAVE PROGRAM.
  161. ENDMODULE. "cancel INPUT

Комментарии