I have an input field which should accept only letters (doesn't matter lower or uppercase). I could write this simply by using co ca (contains only/any) operator. But it's time consuming.
if p_input3 ca '*/ + - ? ! % ( ) = 0123456789'. MESSAGE e000 WITH 'Only letters are allowed. No numbers or special characters'. ENDIF.
It works. But I wanted to check it using regex. I tried this code. But It doesn't work quite well.
DATA: text TYPE string, matcher type REF TO cl_abap_matcher. PARAMETERS: p_input3 TYPE string DEFAULT 'abCD*()fhi' LOWER CASE. matcher = cl_abap_matcher=>create( pattern = `([a-zA-Z])` text = p_input3 ). if matcher = abap_true. MESSAGE e000 with 'Only letters are allowed. No numbers or special characters'. ENDIF.
Could anyone figure it out?
First, your regular expression won’t work. It is only successful when your input contains only a single letter.
This one will work correctly:
* mean repetition. That means a chain of any number of characters which falls into the preceding category. The difference is that * can also mean 0 times, while + means at least one time. So an empty string would match
[a-z]* but not
Then you are checking the reference to the matcher object itself for being equal to abap_true. This won't ever happen, because it will always be a reference to a matcher object, not abap_true or abap_false.
To get the result of a match, you have to not just create the matcher but actually run it by calling its match method.
IF matcher->match( ) = abap_false. MESSAGE e000 with 'Only letters are allowed. No numbers or special characters'. ENDIF.
But there is also a much easier way to do this with the
FIND REGEX command instead of the class cl_abap_matcher:
FIND FIRST OCCURRENCE OF REGEX '[^a-zA-Z]' IN p_input3. IF sy-subrc = 0. MESSAGE 'There is some non-letter in your input.' TYPE 'E'. ENDIF.
^ means negation. So
[^a-zA-Z] means any character which is NOT in the given range.