Prefer Simpler String Functions over Plain Regex
WarningClean ABAP & Modern Syntax#EC PREF_PLAIN_REGEXRemote Enabled
Overview
Prefer Simpler String Functions to Regex.
ATC Finding Message:
Regex pattern contains no metacharacters - use the simple sub / case parameter instead
Rationale
A regex literal that contains no metacharacters (such as . [ { ( ) * + ? ^ $ | or the escape backslash) performs a plain literal search yet pays the regex engine's parsing and backtracking cost. Replace plain literal patterns with the simple, cheaper and more readable sub / case parameters of find, count, replace or contains. Only literal patterns without metacharacters are flagged; textual FIND/REPLACE with REGEX is covered by no-obs-strings.
Non-Compliant
abap
result = replace( val = text
regex = 'HELLO'
with = 'WORLD' ).Compliant
abap
result = replace( val = text
sub = 'HELLO'
with = 'WORLD' ).Suppression
To suppress findings for this rule, append the pseudo-comment to the statement:
abap
" Standard pseudo-comment:
... "#EC PREF_PLAIN_REGEX
" SAP CI_ prefix format:
... "#EC CI_PREF_PLAIN_REGEXRelated Rules
- Avoid Obsolete String and POSIX Regex Statements —
#EC NO_OBS_STRINGS - Prefer Built-in String Functions over SEARCH/SHIFT —
#EC PREF_STR_FUNC - Prefer String Templates over CONCATENATE —
#EC PREF_STR_TMPL
Technical Details
- Native ATC Class:
ZCL_SKYLINT_PREF_PLAIN_REGEX - Implementation File: zcl_skylint_pref_plain_regex.clas.abap