‎2008 Oct 27 4:08 PM
Hi,
I want to build a regular expression to find all namespaces in a xml document (<root xmlns:test="blah" xmlns="blubb">).
My regex looks like this:
((xmlns:.*?=".*?")|(xmlns=".*?"))For some strange reason, the compliler thinks, this statement is invalid. I tested it with different external regex parsers, and it worked perfectly and returned the list of namespaces.
The compiler message says, that there is an error at the position "11" which points to the "?". Removing all ? in the statement, compiling works, but then the result is wrong .
FIND REGEX '((xmlns:.*?=".*?")|(xmlns=".*?"))' IN lv_xml_in
RESULTS lt_result.
Do you have an idea what the issue is?
Thanx,
Sascha
Update: I found the following statement in the Help: *? - Reserved for later enhancements. Which means, that the switch for relaxed behaviour is not working (yet). Do you have an idea, how to restate the expression, to meet my requirement?
Edited by: Sascha Kiefer on Oct 27, 2008 6:31 PM
‎2008 Oct 28 10:43 AM
Hi,
as long as the "relaxed behavior" of RegEx is not supported in the kernel, I use the following work-around:
I look for the first occurence, store it, and remove it from the XML input string (which works perfectly for me, since I want to remove it anyway. If you just want to extract it, I guess you have to use a copy of the original string). I repeat the search and replace until no mor matches are found.
Here's to coding:
DATA: ls_result TYPE match_result,
lv_namespace TYPE string,
lt_namespace TYPE TABLE OF string.
WHILE 1 = 1.
CLEAR: ls_result,
lv_namespace.
FIND FIRST OCCURRENCE OF REGEX '(xmlns:.[^=]*="[^"]*")|(xmlns="[^"]*")'
IN lv_xml_in
IGNORING CASE
RESULTS ls_result.
IF sy-subrc NE 0.
EXIT.
ENDIF.
lv_namespace = lv_xml_in+ls_result-offset(ls_result-length).
APPEND lv_namespace TO lt_namespace.
REPLACE FIRST OCCURRENCE OF lv_namespace IN lv_xml_in WITH ''.
ENDWHILE.