‎2012 Jan 03 7:23 PM
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
‎2012 Jan 03 7:40 PM
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.
‎2012 Jan 03 7:40 PM
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.
‎2012 Jan 03 7:45 PM
Hi Vishnu,
The text can be of any number of words.
when it is 3 words for search how should be my regex be?
‎2012 Jan 03 7:50 PM
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
‎2012 Jan 03 7:56 PM
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
‎2012 Jan 03 8:05 PM
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.
‎2012 Jan 04 2:25 PM
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
‎2012 Jan 04 3:21 PM
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
‎2012 Jan 04 3:35 PM
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.
‎2012 Jan 04 3:38 PM
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_stringThe 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
‎2012 Jan 04 4:03 PM
‎2012 Jan 04 4:09 PM
‎2012 Jan 04 5:28 PM
Hi Vishnu
Thanks a lot for the info...this is something new:):)
Its interesting..
Nabheet
‎2012 Jan 04 6:09 PM
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
‎2012 Jan 04 6:17 PM
Resolved
FIND ALL OCCURRENCES OF REGEX '\bmedia\b|\brent\b|\bfine\b' IN ls_string RESULTS result_tab.
‎2012 Jan 05 7:08 AM
>
> 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
‎2012 Jan 05 9:31 AM
>
> 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.