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

Problem in using find command - Need Pointers

Former Member
0 Likes
1,464

Hi Experts

I am using FIND Command to search for a string in fields of internal table.

Example:

Search string : media rent

When i use find all occurences of search string in 'media rent buy'

it gives me correct result.

But

When i use find all occurences of search string in 'media is fine rent'

It gives sy-subrc 4 what i mean is words media and rent are in the phrase 'media is fine rent'

but i doesnt find that.

Any pointers to it ??

I am using below code

CONCATENATE '(^|[[:blank:][:cntrl:][:punct:]]|$)'

I_SEARCH_STRING

'(^|[[:blank:][:cntrl:][:punct:]]|$)'

INTO I_SEARCH_STRING.

Thanks

Bhanu

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,417

The below regex 'media.*rent' will match media followed by rent with zero or more characters in between.

DATA: ls_string TYPE string VALUE 'media is fine rent'.
FIND FIRST OCCURRENCE OF REGEX 'media.*rent' IN ls_string.

16 REPLIES 16
Read only

Former Member
0 Likes
1,418

The below regex 'media.*rent' will match media followed by rent with zero or more characters in between.

DATA: ls_string TYPE string VALUE 'media is fine rent'.
FIND FIRST OCCURRENCE OF REGEX 'media.*rent' IN ls_string.

Read only

0 Likes
1,417

Hi Vishnu,

The text can be of any number of words.

when it is 3 words for search how should be my regex be?

Read only

0 Likes
1,417

For 3 words, it will be like

'word1.*word2.*word3'

Also remember that the above regex can also match a string like 'word1xyword2abword3', without any blank space before or after each word.

If you want only exact words to match (a word is defined as a sequence of non blank space characters bounded by blank space on either side), then you should use

'\bword1\b.*\bword2\b.*\bword3\b'

or going by your original problem

'\bmedia\b.*\brent\b'

\b means start or end of a word

Read only

0 Likes
1,417

Hey vishnu

Thanks for sharing the info..can you please explain detail about this regex command i have read it many places but did not get it completely..

Thanks

Nabheet

Read only

0 Likes
1,417

Hello Nabheet,

Regular expressions is a comprehensive topic and needs separate study and practice and can't be explained in a single post. It provides a powerful and intelligent way of pattern matching and is highly descriptive.

I can explain the simple regular expressions we used here

dot ( . ) - means any single character

.* matches a string of 0 or more characters (* always applies to the character before it), so .* matches with null string or a string of any length

So

'word1.*word2.*word3'

matches word1 followed by word2 followed by word3 with 0 or any number of characters in between the words. By putting \b on either side of the words we can the restrict the match to only words which have blanks on either side (except beginning or end of string) like 'word1 word2 word3'.

Similarly, .+ matches a string of 1 or more characters, so .+ match with a single character or a string of any length but not null string

There are much more powerful expressions using other meta-characters. Someday I will create a wiki of frequently used regular expressions in ABAP, with explanations and will let everyone on the forums know.

Read only

0 Likes
1,417

To Search for a string in a internal table the format 'word1.word2.word3' works fine with FIND STATEMENT.

But in a situation where the order of the search string doesnt match with data we are searching for it wont work.

For example:

Search string: 'media.rent.fine'.

When i search the above regex in a string like 'media rent is fine' it will search for the words.

But When i search the above regex in a string like 'fine rent media dvd' it will search give a sy-subrc 4.

Experts please suggest how can we search in a string ignoring the order in which they are.

Thanks

Bhanu

Read only

matt
Active Contributor
0 Likes
1,417

Split your input string into and internal table of individual words. Get you search words in a table of individual words. Loop through the latter, and see if the words exist in the former.

This really is VERY basic programming.

matt

Edited by: Matt on Jan 4, 2012 4:21 PM

Read only

0 Likes
1,417

Thanks for the reply matt.

I was thinking to use a regular expression and find the expression in one go rather then searching for each word in a loop.

Read only

0 Likes
1,417

Bhanu,

Regex offers elegant solution for that too. We use the pipe symbol '|' as an OR operator in regular expressions

FIND ALL OCCURRENCES OF REGEX 'media|rent|fine' IN ls_string

The above command matches with both

ls_string = 'fine rent media dvd'

and

ls_string = 'media rent is fine'

Matt,

We can solve a problem that regex can solve anyway by using a elaborate programming logic that adds to maintenance overhead. But what is the point when regex offers a simple solution spanning not more than few lines of code. Problem may be trivial but solution should be intelligent

Read only

0 Likes
1,417

Vishnu,

Thanks to you and Regex:-)

Solved.....

Read only

0 Likes
1,417

Please assign points for helpful answers.

Rob

Read only

0 Likes
1,417

Hi Vishnu

Thanks a lot for the info...this is something new:):)

Its interesting..

Nabheet

Read only

0 Likes
1,417

Vishnu,

FIND ALL OCCURRENCES OF REGEX 'media|rent|fine' IN ls_string works fine but it also search for words related to it.

For example it also give results for 'rented' 'fines'.

I tried using

'(^|[[:blank:][:cntrl:][:punct:]])media|rent|fine([[:blank:][:cntrl:][:punct:]]|$)' but this doesnt work.

Any pointers on this?

Thanks for all your help.

Bhanu

Read only

0 Likes
1,417

Resolved

FIND ALL OCCURRENCES OF REGEX '\bmedia\b|\brent\b|\bfine\b' IN ls_string RESULTS result_tab.

Read only

matt
Active Contributor
0 Likes
1,417

>

> Matt,

>

> We can solve a problem that regex can solve anyway by using a elaborate programming logic that adds to maintenance overhead. But what is the point when regex offers a simple solution spanning not more than few lines of code. Problem may be trivial but solution should be intelligent

That's fair enough, and in this case with the simple regex offered, it's definitely the best solution - I should have seen it myself. (blush). However, I've seen complex regex used and that's where the problems lie. Sometimes it's better to write a simple program, than to use a complex (single) regex - prceisely because of maintenance overhead!

(I was also watching this thread in case it turned into a basic programming issue, but it didn't )

matt

Read only

0 Likes
1,417

>

> That's fair enough, and in this case with the simple regex offered, it's definitely the best solution - I should have seen it myself. (blush). However, I've seen complex regex used and that's where the problems lie. Sometimes it's better to write a simple program, than to use a complex (single) regex - prceisely because of maintenance overhead!

>

> (I was also watching this thread in case it turned into a basic programming issue, but it didn't )

>

> matt

Matt, I understand what you are saying and you bring up an interesting point. Regexes are not very popular, that is precisely because many of the common everyday pattern matching problems that regexes can solve elegantly can also be solved by programming. It is like trying to learn a foreign language when everyone around speaks your mother tongue

However there are many situations where regexes can save us several pages of complex coding as can be seen [here|;

Good commenting practice can help here.

As more and more ABAP developers come across regexes they may not remain esoteric for a long time. Regular expressions are quite old, it is sad that SAP introduced them quite late.

I appreciate you and other moderators, taking moderation very seriously and spending some of your time in keeping the forum highly professional. This is quite unlike many professional boards that I came across.