Application Development 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: 

Pattern Matching : String Processing

Former Member
0 Kudos

Hi,

I have two strings. Say source and destination.

Now I want to find out the percentage which will indicate the number of characters/words match between these 2 strings

i.e. if the 2 strings are exactly same including case sensitivity then the percentage will be 100 %

This matching should be intelligent matching. i.e. if soruce contains 'This is Test' and destination contains 'is this est'

In this case since 'this' and 'is' matches the strings should be taken in the output. also 'Test' and 'est' matches the same should be reflected.

How can I use pattern matching in this case. Does ne fm exists for pattern matching and resulting into the percentage or ne number relecting the

number of characters or words matching between 2 strings.

Regards,

Nitin

3 REPLIES 3

Former Member
0 Kudos

Hello,

What you can do is first seperate one string say source at space into a variable and then use this to find whether there is a match using the lgoical expressions that sap provides.

Logical Expressions - Relational Operators for Character-Like Fields

In the condition "c1 op c2", the relational operation op between the fields c1 and c2 may be any of the operations listed below, but c1 and c2 must be character- like (type C, STRING, N, D, T).

CO (Contains Only):

c1 contains only characters from the string c2.

If c1 or c2 is of type C, the comparison takes into

account the full length of the field, including blanks at the end.

comparison.

If c1 is of type STRING and empty, the result of the

comparison is always positive.

If c2 is of type STRING and empty, the result of the

comparison is always negative, unless c1 is also an empty

string.

If the result of the comparison is negative, the field SY-FDPOS

contains the offset of the first character in c1 which is not

also included in c2.

If the result of the comparison is positive, the system field SY-FDPOS contains the length of c1.

The comparison is case-sensitive.

Examples:

'ABCDE' CO 'XYZ' is false; SY-FDPOS = 0.

'ABCDE' CO 'AB' is false; SY-FDPOS = 2.

'ABCDE' CO 'ABCDE' is true; SY-FDPOS = 5.

CN (Contains Not only):

"c1 CN c2" is equivalent to " NOT ( c1 CO c2 )".

c1 contains not only characters from c2.

If the result of the comparison is positive, the system field SY-FDPOS contains the offset of the first character in c1 which is not also in c2.

If the result of the comparison is negative, the system field SY-FDPOS contains the length of c1.

CA (Contains Any):

c1 contains at least one character from the string c2.

If c1 or c2 is of type C, the comparison takes into account the full length of the field, including blanks at the end.

If c1 or c2 is of type STRING and empty, the result of the comparison is always negative.

If the result of the comparison is positive, the system field SY-FDPOS contains the offset of the first character in c1 which is also in c2.

If the result of the comparison is negative, the system field SY-FDPOS contains the length of c1.

The comparison is case-sensitive.

Examples:

'ABCDE' CA 'CY' is true; SY-FDPOS = 2.

'ABCDE' CA 'XY' is false; SY-FDPOS = 5.

NA (contains Not Any):

"c1 NA c2" is equivalent to "NOT ( c1 CA c2 )".

c1 contains no characters from c2.

SY-FDPOS is set accordingly.

CS (Contains String):

c1 contains the character string c2.

Trailing blanks in c1 and c2 are ignored if the respective field is of type C.

An empty string c2 (i.e., only blanks with type C, or empty string with type STRING) is included in any string c1, including the empty string itself. On the other hand, there is no non-empty string c2 included in an empty string c1.

If the result of the comparison is positive, the system field SY-FDPOS contains the offset of the first character of c2 in c1.

If the result of the comparison is negative, the system field SY-FDPOS contains the length of c1.

The comparison is not case-sensitive.

Examples:

'ABCDE' CS 'CD' is true; SY-FDPOS = 2.

'ABCDE' CS 'XY' is false; SY-FDPOS = 5.

'ABAAA' CS 'AB ' is true; SY-FDPOS = 0.

' ABC' CS ' AB' is true; SY-FDPOS = 1.

'ABC DEF' CS ' ' is true; but: SY-FDPOS = 0,

since ' ' is interpreted as a trailing blank and is thus

ignored.

NS (contains No String):

"c1 NS c2" is equivalent to " NOT ( c1 CS c2 )".

c1 does not contain c2.

SY-FDPOS is set accordingly.

CP (Contains Pattern):

The complete string c1 matches the pattern c2 (c1 "matches" c2).

The pattern c2 can contain ordinary characters and wildcards.

'*' stands for any character string and '+' denotes any character.

If the result of the comparison is positive, the system field SY-FDPOS contains the offset of the first character of c2 in c1. The wildcard character '*' at the beginning of the pattern c2 is ignored when determining the value of SY-FDPOS.

If the result of the comparison is negative, the system field SY-FDPOS contains the length of c1.

Examples:

'ABCDE' CP 'CD' is true; SY-FDPOS = 2.

'ABCDE' CP '*CD' is false; SY-FDPOS = 5.

'ABCDE' CP '+CD' is true; SY-FDPOS = 0.

'ABCDE' CP '+CD*' is false; SY-FDPOS = 5.

'ABCDE' CP 'BD*' is true; SY-FDPOS = 1.

The character '#' has a special meaning. It serves as an escape symbol and indicates that the very next character should be compared "exactly".

This allows you to search for:

- characters in upper or lower case

e.g.: c1 CP '#A#b'

- the wildcard characters '*', '+' themselves

e.g.: c1 CP '#' or c1 CP '#+*'

- the escape symbol itself

e.g.: c1 CP '##'

- blanks at the end of c1

e.g.: c1 CP '*# '

If c2 does not contain the wildcard character '*', the shorter field is padded with "soft blanks" to bring it up to the length of the longer field.

Examples:

'ABC' CP 'ABC ' is true,

'ABC ' CP 'ABC' is true,

but

'ABC' CP 'ABC+' is false,

'ABC' CP 'ABC# ' is false,

because a "soft blank" is neither any character ('+') nor a "real" blank ('# ').

The escape symbol does not affect the length of f2 ('A#a#B' still has the length 3).

The comparison is not case-sensitive.

NP (contains No Pattern):

"c1 NP c2" is equivalent to " NOT ( c1 CP c2 )"

c1 does not match c2.

Using various such patterns you will be able to achieve what you want.

Regards,

Shekhar Kulkarni

FredericGirod
Active Contributor
0 Kudos

Hello Nitin Pawar,

interesting game, but I don't understand the example you give concerning "This is Test", can you give example and pourcentage that you would like to have ?

Frédéric

0 Kudos

Hi Fredrick,

Actually problem goes like this.

In Plant Maintenance (PM) module when users create notifications using IW21, they in Items and Causes tab, they are supposed to select code group which automatically gives a corresponding text for the problem.

But many times users dont find the appropriate code group using F4 and they manually type the problem description and leave the code group blank.

Now since my analysis of problems is based on Code groups I need all code groups to be updated.

Hence I get all notifications for a particular period having code group blank.

Now depending on the string user has entered I will have to find out intelligently associated code groups and percentage of the string matching.

If the percentage comes 100% it is most probable that its only case difference. In other cases I have to infer the percentage depending upon permutations and combinations.

What currently I am doing is splitting the string at SPACE into itab. LOOP at itab and use the pattern matching operators like CO / CP.

But the results are not 100% correct. It also restricts me to the matching at word level.

Hope u have got the scenario.

Cheers,

Nitin