regular expressions

24
2.9.3.12 Expresiones regulares Regular Expressions As of release 7.0, ABAP supports extended regular expressions in accordance with POSIX standard 1003.2. Regular expressions can be used after the addition REGEX of the statements FIND and REPLACE for searching in character strings. The classes CL_ABAP_REGEX and CL_ABAP_MATCHER permit object-oriented use of regular expressions. A regular expression r represents a set of character strings. If text is a character string represented by r, we say that r matches text or that r fits text. Two (different) regular expressions match if they fit the same set of character strings. If you apply a regular expression to a character string text as a search string, then you are searching for matches of the regular expression with substrings of text. In this case, specific special characters of the regular expression do not match characters, but instead match positions, thus influencing the type and number of found locations. - Syntax of regular expressions - Testing regular expressions Note This software uses the Boost Regex Library. Copyright (c) 1998-2004 Dr. John Maddock. 2.9.3.12.1 Sintaxis de expresiones regulares Syntax of Regular Expressions The syntax of regular expressions can be broken down into: - Single character strings - Character string patterns - Search strings - Replacement strings - Simplified regular expressions Using the syntax for single characters and character strings, regular expressions can be created that match whole character strings or substrings of character strings. The syntax for find and replace offers some additional elements that support finding and replacing for substrings in character strings. The special characters that are valid in regular expressions are summarized in: - Special characters in regular expressions. ______________________________________________________________ SAP AG 1 Sistema SAP ______________________________________________________________

Upload: alvaro-olmos

Post on 31-Oct-2014

36 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Regular Expressions

2.9.3.12 Expresiones regulares

Regular Expressions

As of release 7.0, ABAP supports extended regular expressions in accordance with POSIX standard

1003.2. Regular expressions can be used after the addition REGEXof the statements FIND and REPLACEfor searching in character strings. The classes CL_ABAP_REGEX and CL_ABAP_MATCHER permit

object-oriented use of regular expressions.

A regular expression r represents a set of character strings. If text is a character string represented by r ,we say that r matches text or that r fits text . Two (different) regular expressions match if they fit the

same set of character strings.

If you apply a regular expression to a character string text as a search string, then you are searching for

matches of the regular expression with substrings of text . In this case, specific special characters of the

regular expression do not match characters, but instead match positions, thus influencing the type and

number of found locations.

- Syntax of regular expressions

- Testing regular expressions

Note

This software uses the Boost Regex Library. Copyright (c) 1998-2004 Dr. John Maddock.

2.9.3.12.1 Sintaxis de expresiones regulares

Syntax of Regular Expressions

The syntax of regular expressions can be broken down into:

- Single character strings

- Character string patterns

- Search strings

- Replacement strings

- Simplified regular expressions

Using the syntax for single characters and character strings, regular expressions can be created that match

whole character strings or substrings of character strings. The syntax for find and replace offers some

additional elements that support finding and replacing for substrings in character strings.

The special characters that are valid in regular expressions are summarized in:

- Special characters in regular expressions.

______________________________________________________________SAP AG 1

Sistema SAP______________________________________________________________

Page 2: Regular Expressions

2.9.3.12.1.1 Patrón de carácter individual

Single Character Strings

Single characters are represented by literal characters or operators. If preceded by a backslash \ , a specialcharacter of an operator is interpreted as a literal character. This applies in particular for the backslash \itself, so that the regular expression \\ is the same as the single character \ . If the backslash is followedby a literal character, the backslash is ignored and not taken into account.

Literal characters

A literal character is a character that is not a special character, or a special character preceded by a

backslash \ , or enclosed between \Q ... \E . As a search string, a literal character matches the same

single character exactly.

Note

Upper/lower case sensitivity can be controlled for the corresponding commands or methods.

Examples

The following table shows some results of the test program, if the value ' ' is passed to

ignore_case .

Pattern Text Match

A A X

A a -

\. . X

A AB -

AB AB X

The regular expression 'AB' is a concatenation of two expressions for single characters.

Operators for single characters

These operators are made up of the special characters . , [ , ] , ^ and - , whereby the last two only act asspecial characters at specific positions within [ ] . The special characters can be made into literal

characters using the prefix \ .

Placeholders for single characters

The special character . is a placeholder for any single character. The operator \C has the same effect as

the special character . . A regular expression . or \C matches exactly one single character.

Examples

The following table shows some results of the test program, , whereby the value transferred at

ignore_case can be any value.

Pattern Text Match

. A X

\C a X

______________________________________________________________SAP AG 2

Sistema SAP______________________________________________________________

Page 3: Regular Expressions

. AB -

.. AB X

The regular expression '..' is a concatenation of two expressions for single characters.

Self-defined sets for single characters

The special characters [ ] can be set around any number of literal characters or names for character

classes (see below), and thus define a set of literal characters. A regular expression [ ...] matches exactly

one single character that is listed as a literal character within the brackets, or which is contained in a

specified character class. At least one literal character or one name for a character class (see below) must

be contained within the brackets. A character [ or ] , which is positioned directly after the opening

bracket, is interpreted as a literal character. Some of the special characters that start with a backslash, such

as \A or \Q , lose their special function within sets, and are interpreted as the simple literal character A or

Q.

Examples

The following table shows some results of the test program.

Pattern Text Match

[ABC] B X

[ABC] ABC -

[AB][CD] AD X

[\d] 9 X

The regular expression [AB][CD] is a concatenation of two expressions for single characters.

Negation of a self-defined set for single characters

If the character ^ is the first character in a self-defined set for single characters and is listed directly after

[ , it acts as a special character and negates the rest of the set of literal characters or character classes. A

regular expression [^ ...] matches exactly one single character that is not listed within the brackets as a

literal character, or is not contained in a specified character class. A character ^ that is not listed directly

after [ acts as a literal character.

Examples

The following table shows some results of the test program.

Pattern Text Match

[^ABC] B -

[^ABC] Y X

[^ABC] ABC X

[^A][^B] BA X

[A^B] ^ X

The regular expression [^A][^B] is a concatenation of two expressions for single characters.

Ranges in a self-defined set for single characters

If the character - is between two literal characters, it acts as a special character and defines a range

between the literal characters. The range is the set of characters that is enclosed by literal characters in the

______________________________________________________________SAP AG 3

Sistema SAP______________________________________________________________

Page 4: Regular Expressions

code page of the current operating system. A regular expression [ ...- ...] matches exactly one single

character that is within the defined range. A character - , which is not between two literal characters, actsas a literal character. A literal character can not be part of two ranges, for example, 'a-z-Z' is not a

regular expression.

Examples

The following table shows some results of the test program.

Pattern Text Match

[A-Za-z0-9] B X

[A-Za-z0-9] 5 X

[A-Za-z0-9] # -

[A-Za-z0-9] - -

[A-Za-z0-9-] - X

In the last expression, the closing - does not act as a special character.

Character classes

Within sets for single characters defined with [ ] , predefined platform-independent and

language-independent character classes can be specified:

- [:alnum:]Set of all alphanumeric characters

- [:alpha:]Set of all upper and lower case letters including language-specific special characters (umlauts,

accents, diphthongs)

- [:blank:]Blank characters and horizontal tabs

- [:cntrl:]Set of all control characters

- [:digit:]Set of all digits 0 to 9

- [:graph:]Set of all graphic special characters

- [:lower:]Set of all lower case letters including language-dependent special characters (umlauts, accents,

diphthongs)

- [:print:]Set of all displayable characters

- [:punct:]Set of all punctuation characters

- [:space:]Set of all blank characters, tabs, and carriage feeds

______________________________________________________________SAP AG 4

Sistema SAP______________________________________________________________

Page 5: Regular Expressions

- [:unicode:]Set of all characters with a character representation larger than 255 (only in Unicode systems)

- [:upper:]Set of all upper case letters including language-dependent special characters (umlauts, accents,

diphthongs)

- [:word:]Set of all alphanumeric characters including underscore _.

- [:xdigit:]Set of all hexadecimal digits (0-9, A-F and a-f )

Note

Character classes only act within [ ] as specified. A regular expression [:digit:] does not define the

set of all digits, but instead defines a character set consisting of : , d, g, i and t . To specify the set of all

digits, the regular expression [[:digit:]] is used.

Examples

The following table shows some results from the test program, if the value ' ' is passed to

ignore_case .

Pattern Text Match

[[:alnum:]] a X

[[:alnum:]] ; -

[[:alpha:]] 1 -

[[:digit:][:punct:]] 4 X

[[:digit:][:punct:]] . X

[[:lower:]] â X

[[:upper:]] Ä X

Abbreviations for character sets

For frequently used character sets, specific operators are available as abbreviations:

Character set Abb. Meaning

[[:digit:]] \d Placeholder for a digit

[^[:digit:]] \D Placeholder for a non-digit

[[:lower:]] \l Placeholder for a

lower-case letter

[^[:lower:]] \L Placeholder for a character

that is not a lower-case letter

[[:space:]] \s Placeholder for a blank

character

[^[:space:]] \S Placeholder for characters

other than blank characters

[[:upper:]] \u Placeholder for an

upper-case letter

______________________________________________________________SAP AG 5

Sistema SAP______________________________________________________________

Page 6: Regular Expressions

[^[:upper:]] \U Placeholder for a character

that is not an upper-case letter

[[:word:]] \w Placeholder for an

alphanumeric character including underscore _

[^[:word:]] \W Placeholder for a

non-alphanumeric character except for underscore _

Note

If upper/lower case is not taken into account in the ABAP statement FIND and REPLACEor whengenerating an object of the class CL_ABAP_REGEX, then \l and \u are equivalent to [[:alpha:]]and \L , and \U is equivalent to [^[:alpha:]] . The special characters \w , \u , \l , \d , \s can also be

listed within sets [ ...] . Use of the special characters \W, \U , \L , \D , \S within sets is not permitted and

triggers an exception CX_SY_INVALID_REGEX.

Examples

The following table shows some results of the test program, if the ' ' is transferred to ignore_case .

Pattern Text Match

\d 4 X

\D ; X

\l u X

\l U -

\L s X

\s X

\S # X

\u U X

\U , X

\w A X

\w 8 X

\W : X

\W _ -

Equivalence classes

The operators [..] and [==] are reserved for later language enhancements and trigger the exception

CX_SY_INVALID_REGEX if used in sets.

______________________________________________________________SAP AG 6

Sistema SAP______________________________________________________________

Page 7: Regular Expressions

2.9.3.12.1.2 Patrón de strings

Character String Patterns

Character strings are represented by concatenations or operators.

Concatenations

Concatenations are valid regular expressions that are written after each other. IF r and s are regular

expressions, the concatenation rs matches all character strings that can be formed from the concatenation

of character strings that match r and s .

Examples

The following table shows some results of the test program.

Pattern Text Match

H[aeu]llo Hallo X

H[aeu]llo Hello X

H[aeu]llo Hullo X

H[aeu]llo Hollo -

H[aeu]llo is the concatenation of five regular expressions for single characters.

Operators for character strings

These operators are made up of the special characters { , } , * , +, ?, | , ( , ) and \ . The special characterscan be made into literal characters using the prefix \ or by enclosing with \Q ... \E .

Concatenation operators

The operators {n} , {n,m} , * , + and ? (whereby n and mare natural numbers, including zero) can be

written directly after a regular expression r , and thus generate concatenations rrr... of the regular

expression:

- The regular expression r{n} is equivalent to a n-fold concatenation of r . The regular expressionr{0} matches an empty character string, and therefore also the offset before the first character of a

character string, the spaces between the characters in character strings, and the offset after the last

character in a character string.

- The regular expression r{n,m} is equivalent to at least n and a maximum of mconcatenations of r .The value of n must be smaller than or equal to the value of m. The expression r{n,} is equivalent

to at least n concatenations of r .

- The regular expression r? is equivalent to r{0,1} , which means the expression r or the empty

character string.

- The regular expression r* is equivalent to r{0,} , i.e. a concatenation of r of any length, including

the empty character string. When using subgroups (see below), and in a text search, r* matches the

longest possible substring (greedy behavior).

- The regular expression r+ is equivalent to r{1,} , i.e. a concatenation of any length of r excluding

the empty character string. When using subgroups, and in a text search, r+ matches the longest

possible substring (greedy behavior).

______________________________________________________________SAP AG 7

Sistema SAP______________________________________________________________

Page 8: Regular Expressions

- The regular expressions r{n,m}? , r*? and r+? are reserved for later language enhancements

(non-greedy behavior) and currently trigger the exception CX_SY_INVALID_REGEX.

Example

The following table shows some results from the test program.

Pattern Text Match

Hel{2}o Hello X

H.{4} Hello X

.{0,4} Hello -

.{4,} Hello X

.+H.+e.+l.+l.+o.+ Hello -

x*Hx*ex*lx*lx*ox* Hello X

l+ ll X

Subgroups

The operators ( ... ) and (?: ... ) group concatenations of regular expressions together into one entity

and thus influence the range of effectiveness of other operators such as * or | , which act on this entity. In

this case, the regular expressions (r) and (?:r) match the regular expression r .

Examples

The following table shows some results of the test program.

Pattern Text Match

Tral+a Tralala -

Tr(al)+a Tralala X

Tr(?:al)+a Tralala X

In the first expression, the concatenation with the operator + acts on the literal character l , in the secondand third expressions, it acts on the subgroup al .

Subgroups with registration

The operator ( ... ) acts in the same way as (?: ... ) in the formation of subgroups. In addition, when

comparing the regular expression with a character string, the substrings that match the subgroups ( ... ) of

the expression, are stored sequentially in registers. In this process, an operator \1 , \2 , \3 , ... is assigned

to each subgroup, which can be listed within the expression after its subgroup, and thus acts as a

placeholder for the character string stored in the corresponding register. In text replacements, the special

characters $1 , $2 , $3 , ... can be used to access the last assignment to the register.

The number of subgroups and registers is only limited by the capacity of the platform.

Note

The addition SUBMATCHESof the statements FIND and REPLACEand the eponymous column of the

results table filled using the addition RESULTScan be used to access the content of all registers for a

found location. The the class CL_ABAP_MATCHER contains the method GET_SUBMATCH for this

purpose.

______________________________________________________________SAP AG 8

Sistema SAP______________________________________________________________

Page 9: Regular Expressions

Examples

The following table shows some results of the test program.

pattern text match

(["']).+\1 "Hello" X

(["']).+\1 'Hello' X

(["']).+\1 'Hello" -

The concatenation (["']).+\1 matches all text strings of which the first character is " or ' and the

last character is the same as the first. A call matcher->get_submatch( index = 1 ) returns the

values " , " or ' for all three cases.

Reserved enhancements

The character string (? ... ) is generally reserved for later language enhancements, and with the exception

of the already supported operators (?: ...) , (?= ...) and (?! ...) , triggers the exceptionCX_SY_INVALID_REGEX.

Alternatives

The operator | can be written between two regular expressions r and s , and thus generates a single

regular expression r|s , which matches both r and s .

Note

Concatenations and other operators are more binding than | , which means that r|st and r|s+ are

equivalent to r|(?:st) or r|(?:s+) , and not to (?:r|s)t or (?:r|s)+ .

Examples

The following table shows some results of the test program.

Pattern Text Match

H(e|a|u)llo Hello X

H(e|a|u)llo Hollo -

He|a|ullo Hallo -

He|a|ullo ullo X

Literal characters

The operators \Q ... \E form a character string of literal characters from all enclosed characters. Special

characters have no effect in this character string.

The following table shows some results of the test program.

Pattern Text Match

.+\w\d Special: \w\d -

.+\\w\\d Special: \w\d X

.+\Q\w\d\E Special: \w\d X

______________________________________________________________SAP AG 9

Sistema SAP______________________________________________________________

Page 10: Regular Expressions

______________________________________________________________SAP AG 10

Sistema SAP______________________________________________________________

Page 11: Regular Expressions

2.9.3.12.1.3 String de búsqueda

Search Strings

One of the principle uses of regular expressions is the search for (and subsequent replacement of)

substrings in character strings. In general, a user is interested in a specific selection of character strings

that match a regular expression. In ABAP, the search of regular expressions is realized using the addition

REGEXof the statement FIND, whereby the found substrings are determined with no overlaps according

to the leftmost-longest rule.

Leftmost-longest rule

First, the substring is determined that is the furthest to the left in the character string, and which matches

the regular expression ("leftmost"). If there are several substrings, the longest sequence is chosen

("longest"). This procedure is then repeated for the remaining sequence after the found location.

Example

For the regular expression d*od* , five substrings are found in doobedoddoo : do at offset 0, o at offset

2, dodd at offset 5, o at offset 9 and o at offset 10.

DATA result_tab TYPE match_result_tab.FIND ALL OCCURRENCES OF regex 'd*od*' IN 'doobedodd oo' RESULTS result_tab.

Operators for search strings

The following operators support searching in character strings. These operators are made up of the special

characters ^ , $, \ , <, >, ( , ) , =and ! . The special characters can be made into literal characters using the

prefix \ .

Start and end of a character string

The operators ^ and $ act as anchor characters for the offset before the first character of a line and the

offset after the last character of a line. If the character string to be searched contains control characters

such as a line feed, it is interpreted as consisting of several lines.

The operators \A and \Z have the same effect as ^ and $, but always refer to the whole character string

instead of to single lines.

Note

The operators ^ , $ and \A , \Z behave differently if control characters are present. Within ABAP

programs, these control characters normally occur only for importing externally generated data records.

Example

The following search finds Smile at the start of the first line and at the end of the last line of the internal

table text_tab .

DATA text(10) TYPE c.DATA text_tab LIKE TABLE OF text.

DATA result_tab TYPE match_result_tab.

______________________________________________________________SAP AG 11

Sistema SAP______________________________________________________________

Page 12: Regular Expressions

APPEND 'Smile' TO text_tab.APPEND ' Smile' TO text_tab.APPEND ' Smile' TO text_tab.APPEND ' Smile' TO text_tab.APPEND ' Smile' TO text_tab.APPEND ' Smile' TO text_tab.

FIND ALL OCCURRENCES OF regex '^(?:Smile)|(?:Smile) $' IN TABLE text_tab RESULTS result_tab.

Start and end of a word

The operator \< fits at the start of a word and the operator \> fits at the end of a word. The operator <\bfits at both the beginning and the end of a word. A word is defined as an uninterrupted sequence of

alphanumeric characters.

Example

The following search finds the three words One, two and 3. Instead of the expression

\<[[:alnum:]]+\> , \b[[:alnum:]]+\b can also be used.

DATA text TYPE string.DATA result_tab TYPE match_result_tab.

text = `One, two, 3!`.

FIND ALL OCCURRENCES OF regex '\<[[:alnum:]]+\>' IN text RESULTS result_tab.

Preview conditions

The operator (?= ...) defines a regular expression s as a subsequent condition for a previous regular

expression r . The regular expression r(?=s) has the same effect in a search as the regular expression r ,if the regular expression s matches the substring that immediately follows the substring found with r .

The operator (?! ...) acts in the same way as (?= ...) , with the difference that r(?!s) matches the

substring for r if s does not match the subsequent substring.

Note

The substring found by the preview s is not a part of the match found by r(?=s) .

Example

The following search finds the substring la at offset 7.

DATA text TYPE string.DATA result_tab TYPE match_result_tab.

text = `Shalalala!`.

FIND ALL OCCURRENCES OF REGEX '(?:la)(?=!)' IN text RESULTS result_tab.

______________________________________________________________SAP AG 12

Sistema SAP______________________________________________________________

Page 13: Regular Expressions

______________________________________________________________SAP AG 13

Sistema SAP______________________________________________________________

Page 14: Regular Expressions

2.9.3.12.1.4 Patrón de sustitución

Replace String

After searching, the replacement of substrings in character strings is the most important application of

regular expressions. When replacing, the found locations of a search (or the substrings that match a

regular expression), are replaced by one or more different character strings. In ABAP, the replacement is

realized using regular expressions with the addition REGEXof the statement REPLACE.

In contrast to normal text replacements, when regular expressions are used, operators can be used in the

replacement text that refer to the relevant found location.

Operators for replacement texts

The following operators can be specified in the replacement text. These operators are made up of the

special characters $, &, ` and ´ . The special characters can made into literal characters by the prefix \ .

Addressing the whole found location

The operators $0 and $& can be entered in the replacement text as placeholders for the whole current

found location.

Example

After replacement, text has the content Yeah Yeah Yeah! .

DATA: text TYPE string.

text = `Yeah!`.

REPLACE REGEX `\w+` IN text WITH `$0 $0 $&`.

Addressing the registers of subgroups

The operators $1 , $2 , $3 , ... can be used in the replacement text as placeholders for the character strings

stored in the registers of subgroups for the current found location. If the n-th subgroup is not available, or

it is not supplied with a value in the match, the corresponding operator $n is replaced by the empty

character string.

Example

After replacement, text has the content Roll'n'Rock .

DATA: text TYPE string.

text = `Rock'n'Roll`.

REPLACE REGEX `(\w+)(\W\w\W)(\w+)` IN text WITH `$3 $2$1`.

Addressing the text before the found location

The operator $` can be used in the replacement text as a placeholder before the current found location. If

several found locations are replaced using REPLACE ALL OCCURRENCES, $` contains the unchanged

text from the beginning of the text to the start of the found location, for every found location.

______________________________________________________________SAP AG 14

Sistema SAP______________________________________________________________

Page 15: Regular Expressions

Example

After replacement, text has the content again and again .

DATA: text TYPE string.

text = `again and`.

REPLACE REGEX 'and' IN text WITH '$`$0 $`'.

Addressing the text after the found location

The operator $' can be used in the replacement text as a placeholder for the whole text after the current

found location.

Example

After replacement, text has the content and again .

DATA: text TYPE string.

text = `again and`.

REPLACE REGEX `again ` IN text WITH `$' $0`.

______________________________________________________________SAP AG 15

Sistema SAP______________________________________________________________

Page 16: Regular Expressions

2.9.3.12.1.5 Expresiones regulares simplificadas

Simplified Regular Expressions

In addition to the regular expressions according to the extended POSIX standard IEEE 1003.1, the class

CL_ABAP_REGEX also offers an alternative type of simplified regular expressions with restricted

functionality. These simplified regular expressions (also known as simplified expressions) do not support

all POSIX operators and use a slightly varying syntax. The semantics of regular expressions and

simplified expressions is, however, the same.

Simplified syntax

The following table provides an overview of the syntax differences between regular expressions and

simplified regular expressions.

Regular syntax Simplified syntax

* *

+ not supported

{ } \{ \}

( ) \( \)

[ ] [ ]

| not supported

(?= ) (?! ) not supported

(?: ) not supported

Note

Regular expressions with simplified syntax can only be used within the class CL_ABAP_REGEX. If the

value 'X' is transferred for the input parameter simple_regex , the regular expression is viewed

according to the simplified syntax. By default, the syntax is used according to the extended POSIX

standard. If the simplified syntax is to be used in the statements FIND or REPLACE, an object must be

transferred.

Example

Regular expression Simplified expression Matches

(.) - a

- (.) (a)

\(.\) - (a)

- \(.\) a

______________________________________________________________SAP AG 16

Sistema SAP______________________________________________________________

Page 17: Regular Expressions

Example

The following program determines all consecutive repeated double characters.

DATA: regex TYPE REF TO cl_abap_regex, res TYPE match_result_tab, text TYPE string.

CREATE OBJECT regex EXPORTING pattern = '\(.\)\1' simple_regex = 'X'

FIND ALL OCCURRENCES OF REGEX regex IN text RESULTS res.

______________________________________________________________SAP AG 17

Sistema SAP______________________________________________________________

Page 18: Regular Expressions

2.9.3.12.1.6 Caracteres especiales en expresiones regula res

Special Characters in Regular Expressions

The following tables summarize the special characters in regular expressions:

Escape characterSpecial character Meaning

\ Escape character for special characters

Special character for single character stringsSpecial character Meaning

. Placeholder for any single character

\C Placeholder for any single character

\d Placeholder for any single digit

\D Placeholder for any character other than a digit

\l Placeholder for any lower-case letter

\L Placeholder for any character other than a lower-case

letter

\s Placeholder for a blank character

\S Placeholder for any character other than a blank

character

\u Placeholder for any upper-case letter

\U Placeholder for any character other than an upper-case

letter

\w Placeholder for any alphanumeric character including

_

\W Placeholder for any non-alphanumeric character

except for _

[ ] Definition of a value set for single characters

[^ ] Negation of a value set for single characters

[ - ] Definition of a range in a value set for single

characters

[ [:alnum:] ] Description of all alphanumeric characters in a value

set

[ [:alpha:] ] Description of all letters in a value set

[ [:blank:] ] Description for blank characters and horizontal

tabulators in a value set

[ [:cntrl:] ] Description of all control characters in a value set

[ [:digit:] ] Description of all digits in a value set

[ [:graph:] ] Description of all graphic special characters in a value

set

[ [:lower:] ] Description of all lower-case letters in a value set

______________________________________________________________SAP AG 18

Sistema SAP______________________________________________________________

Page 19: Regular Expressions

[ [:print:] ] Description of all displayable characters in a value set

[ [:punct:] ] Description of all punctuation characters in a value set

[ [:space:] ] Description of all blank characters, tabulators, and

carriage feeds in a value set

[ [:unicode:] ] Description of all Unicode characters in a value set

with a code larger than 255

[ [:upper:] ] Description of all upper-case letters in a value set

[ [:word:] ] Description of all alphanumeric characters in a value

set, including _

[ [:xdigit:] ] Description of all hexadecimal digits in a value set

\a \f \n \r \t \v Diverse platform-specific control characters

[..] Reserved for later enhancements

[==] Reserved for later enhancements

->More

Special characters for character string patternsSpecial character Meaning

{n} Concatenation of n single characters

{n,m} Concatenation of at least n and a maximum of msingle characters

{n,m}? Reserved for later enhancements

? One or no single characters

* Concatenation of any number of single characters

including 'no characters'

*? Reserved for later enhancements

+ Concatenation of any number of single characters

excluding 'no characters'

+? Reserved for later enhancements

| Linking of two alternative expressions

( ) Definition of subgroups with registration

(?: ) Definition of subgroups without registration

\1 , \2 , \3 ... Placeholder for the register of subgroups

\Q ... \E Definition of a string of literal characters

(? ... ) Reserved for later enhancements

->More

Special characters for search stringsSpecial character Meaning

^ Anchor character for the start of a line

\A Anchor character for the start of a character string

$ Anchor character for the end of a line

______________________________________________________________SAP AG 19

Sistema SAP______________________________________________________________

Page 20: Regular Expressions

\Z Anchor character for the end of a character string

\< Start of a word

\> End of a word

\b Start or end of a word

\B Space between characters within a word

(?= ) Preview condition

(?! ) Negated preview condition

->More

Special characters for replacement textsSpecial character Meaning

$0 , $& Placeholder for the whole found location

$1 , $2 , $3 ... Placeholder for the register of subgroups

$` Placeholder for the text before the found location

$' Placeholder for the text after the found location

->More

______________________________________________________________SAP AG 20

Sistema SAP______________________________________________________________

Page 21: Regular Expressions

2.9.3.12.2 Examinar expresiones regulares

Testing Regular Expressions

The following program section can be used for testing regular expressions:

DATA: matcher TYPE REF TO cl_abap_matcher, match TYPE c LENGTH 1.

matcher = cl_abap_matcher=>create( pattern = .. . ignore_case = .. . text = .. . ).

match = matcher->match( ).

The data object match contains the value "X" if the regular expression transferred to pattern matches

the character string transferred to text .

The following program has the same functions, but works using explicit generation of an object of the

class CL_ABAP_REGEX. If the same regular expression is used several times for different texts, this

form is better for performance than the shorter version above.

DATA: regex TYPE REF TO cl_abap_regex, matcher TYPE REF TO cl_abap_matcher, match TYPE c LENGTH 1.

CREATE OBJECT regex EXPORTING pattern = ... ignore_case = ... .

matcher = regex->create_matcher( text = ... ).

match = matcher->match( ).

______________________________________________________________SAP AG 21

Sistema SAP______________________________________________________________

Page 22: Regular Expressions

Expresiones regulares

Page 23: Regular Expressions

______________________________________________________________

CopyrightCopyright 2011 SAP AG. Reservados todos los derechos.

Queda prohibido el traspaso o la reproducción de esta documentación o de alguna de suspartes sin la autorización por escrito de SAP AG, sea cual sea el fin y la forma. Lainformación contenida en esta documentación podrá modificarse o ampliarse sin previo aviso.

SAP es una marca registrada de SAP AG.

Todos los demás productos que se citan en esta documentación son marcas registradas o noregistradas de las empresas respectivas.

Page 24: Regular Expressions

2.9.3.12 Expresiones regulares 12.9.3.12.1 Sintaxis de expresiones regulares 12.9.3.12.1.1 Patrón de carácter individual 22.9.3.12.1.2 Patrón de strings 72.9.3.12.1.3 String de búsqueda 112.9.3.12.1.4 Patrón de sustitución 142.9.3.12.1.5 Expresiones regulares simplificadas 162.9.3.12.1.6 Caracteres especiales en expresiones regulares 182.9.3.12.2 Examinar expresiones regulares 21

______________________________________________________________SAP AG iii

Información release Índice de contenido SAP AG______________________________________________________________