The requirement is that I use some ABAP function to read a string from the database, and the content of the string is a web page.
Page forms contain many hidden input fields. My task is to resolve the name for svyValueGuid input field value: FA163EEF573D1ED89E89C7FE5E7C4715
The simplest and most crude way to do this is: The FIND FIRST OCCURRENCE keyword of ABAP is used to FIND the offset of svyValueGuid and the FIRST offset >. This problem is simplified as the substring type = “hidden” value = “FA163EEF573D1ED89E89C7FE5E7C4715”, this problem is much more simple. But this approach is cumbersome and redundant.
Is there a faster way? That is to use ABAP regular expression, or regular expression.
Take a look at the following test code:
REPORT ztest_interface. DATA: lv_input TYPE string, reg_pattern TYPE string. lv_input = `<body>` && `<div class="Title">Jerry's Programming Skill survey</div>` && `<form action="Survey.htm? sap-client=001">` && `<input name="svyApplicationId" type="hidden" value="CRM_SURVEY_ACTIVITY">` && `<input name="svyValueGuid" type="hidden" value="FA163EEF573D1ED89E89C7FE5E7C4715">` && `<input name="SurveyId" type="hidden" value="JERRY_TEST">` && `<div Id="" class="Section1">` && `</form></body>`. reg_pattern = '.*svyValueGuid(? :.*)value="(.*)">.*SurveyId.*'. TRY. DATA(lo_regex) = NEW cl_abap_regex( pattern = reg_pattern ). DATA(lo_matcher) = lo_regex->create_matcher( EXPORTING text = lv_input ). IF lo_matcher->match( ) <> abap_true. WRITE:/ 'fail in input scan! '. RETURN. ENDIF. DATA(lt_reg_match_result) = lo_matcher->find_all( ). READ TABLE lt_reg_match_result ASSIGNING FIELD-SYMBOL(<match>) INDEX 1. READ TABLE <match>-submatches ASSIGNING FIELD-SYMBOL(<sub>) INDEX 1. data(lv_sub) = lv_input+<sub>-offset(<sub>-length). WRITE:/ 'result: ', lv_sub. CATCH cx_root INTO DATA(cx_root). WRITE:/ cx_root->get_text( ). RETURN. ENDTRY.Copy the code
Execution Result:
SvyValueGuid (? SvyValueGuid (? 🙂 value = “(.)” >. * SurveyId.
The 32-bit GUID value is captured by capturing the grouping operator, a pair of parentheses. This solution has less code than FIND FIRST OCCURANCE.
For more of Jerry’s original technical articles, please follow the public account “Wang Zixi” or scan the following QR code: