SAP REGEX XPATH - Syntax



Get Example source ABAP code based on a different SAP table
  


VERSION 5 IN

ABAP_REGEX - XPath Syntax
Regular expressions with XPath syntax can be specified behind the argument xpath of built-in functions for strings. Objects for XPath regular expressions can be created with the factory method CREATE_XPATH2 of the system class CL_ABAP_REGEX to be used in statements FIND and REPLACE or with the system class CL_ABAP_MATCHER.
Currently, there is no detailed description of the XPath syntax for regular expressions in the ABAP keyword documentation.
For a short syntax overview, see Special Characters in XPath Regular Expressions
For the complete documentation, refer to XQuery 1.0 and XPath 2.0 Functions and Operators.
A regular expression in XPath syntax can be compiled in a normal and extended mode. In the extended mode, most unescaped whitespace (blanks and line breaks) of the pattern are ignored outside character classes and comments can be placed behind #. In ABAP built-in functions, the extended mode is switched on by default and can be switched off with (?-x) in the regular expression. When using CL_ABAP_REGEX, it can be switched by the parameter EXTENDED of method CREATE_XPATH2.



Latest notes:

A regular expression with XPath syntax cannot be specified directly as a character string in the statements FIND and REPLACE, but you can use objects of CL_ABAP_REGEX that are created with method CREATE_XPATH2 with the addition REGEX instead.
NON_V5_HINTS
The test and demonstration program DEMO_REGEX allows XPath syntax to be tested by selecting XPath.
ABAP_HINT_END

ABAP_EXAMPLE_VX5
A special feature of XPath regular expressions is the subtraction of character sets. In the following example, the letters a to c are subtracted from character set BasicLatin and the first match is d at offset 3.
ABEXA 01475
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
Compared to PCRE, XPath regular expressions allow the escape character not only in front of special characters. The match function with parameter xpath finds x while the match function with parameter pcre does not. Accordingly the first FIND statement returns in sy-subrc the value 0 while the second FIND statement returns 4.
ABEXA 01474
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
XPath regular expression
ABAP_EXAMPLE_END