Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Validate String with a pattern

Former Member
0 Likes
2,740

How can we validate the string with a pattern?

eg. Value should always be of the format C-DDD,

where C is a character (A-Z or a-z) and

D represents a digit(0-9)

Valid forms are A-001, A-987, Z-098 and

not valid forms are A-01, 1-A01, 1- 0A0, A-A01, A-0001

Say internal table contains values like this.

F1 F2 F3

1 00001 A-001

2 00001 A-A01

3 00001 B-909

4 00001 Z-01

5 00001 k-0001

Valid records are 1 and 3.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,380
LOOP AT itab.

  IF itab-f3+0(1) CA sy-abcde.
    IF itab-f3+1(1) EQ '-'.
      IF itab-f3+2(3) CO '0123456789'.
*     Valid
      ELSE.
* Invalid
      ENDIF.
    ELSE.
* Invalid
    ENDIF.
  ELSE.
* Invalid.
  ENDIF.
ENDLOOP.
4 REPLIES 4
Read only

Former Member
0 Likes
1,381
LOOP AT itab.

  IF itab-f3+0(1) CA sy-abcde.
    IF itab-f3+1(1) EQ '-'.
      IF itab-f3+2(3) CO '0123456789'.
*     Valid
      ELSE.
* Invalid
      ENDIF.
    ELSE.
* Invalid
    ENDIF.
  ELSE.
* Invalid.
  ENDIF.
ENDLOOP.
Read only

0 Likes
1,380

Is there any other possible way of coding it, this actually leads to performance issue.

or say for example if the pattern is C-LOCATION or C-location?

where c can be (A-Z or a-z)...

Read only

Former Member
0 Likes
1,380

use this logic



data: char,
         num type n,
         str(4),
        strn(6).

char = 'A'. " u can take from user as well
num = 1000.
str = num.

concatenate char str into strn." u can also use saperate key word as well

Regards

Prakash Varun

Read only

Former Member
0 Likes
1,380

Hi,

you can use regular expressions for this, e.g.:


  FIND ALL OCCURRENCES OF REGEX '[a-z,A-Z]-[0-9]{3}[ [:blank:] ]'
       IN TABLE t_data
       RESPECTING CASE
       RESULTS t_results.

Note that in the above example all fields in the table will be checked. If this is not practical for your use you can use a LOOP and FIND on the table field.

Note that line 5 in your example also matches the pattern you have given. If you really do not want to see this as valid you will have to use the pattern: '[A-Z]-[0-9][ [:blank:] ]'. The addition of [ [:blank:] ] is only needed if your field is longer than the pattern, i.e. 5 characters.

Good luck,

Gert.

Edit: This will also be pretty efficient.

Edited by: Gert Beukema on Jul 15, 2008 10:36 AM