‎2010 Nov 03 1:19 AM
Hi Experts.
I have a text (from Read_text) which comes like this : ....
<H>... </> ....<T>...</>Now in the closing </>, I want to put back the alphabet that started angular bracket. So above should become like this:
...<H>...</H>.... <T>...</T>
Although I have made a logic for it, I feel a REGEX can do the job for me in 1-2 lines and would be much more efficient, since my text can be very long. But unfortunately my REGEX knowledge is too weak to do it.
Any advice please?
Thanks in adv.
Edited by: Aishi A on Nov 3, 2010 9:30 AM
‎2010 Nov 03 1:59 AM
The following should do the trick assuming that S is a character type variable holding your data:
replace all occurrences of regex '<(H|T)>([^<]*)</>' in S with '<$1>$2</$1>'.
Check out the online help on [regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX.htm] to understand what's going on (or for a quicker way access directly the page [special characters in regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX_SPECIALS.htm]). I leave it up to you to figure out the limitations and possible required adaptions to make it work for all your input cases (e.g. if you want it more generic start with <(.+)> instead of the specific H or T)...
‎2010 Nov 03 1:59 AM
The following should do the trick assuming that S is a character type variable holding your data:
replace all occurrences of regex '<(H|T)>([^<]*)</>' in S with '<$1>$2</$1>'.
Check out the online help on [regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX.htm] to understand what's going on (or for a quicker way access directly the page [special characters in regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX_SPECIALS.htm]). I leave it up to you to figure out the limitations and possible required adaptions to make it work for all your input cases (e.g. if you want it more generic start with <(.+)> instead of the specific H or T)...
‎2010 Nov 03 4:53 AM
Thanks Harald.
I tried this: REPLACE ALL OCCURRENCES OF REGEX '<(.+)>([^<])</>' IN ls_lines WITH '<$1>$2</$1>', but it replaces the string with '', that's it.
What can be going wrong?
‎2010 Nov 03 11:55 AM
Please try to check your posting after submitting it - I'm not quite sure if the forum software interpreted some of your coding as markup and thus messed up your message...
Anyhow, my comment on a more generic replacement was a bit quick and thus wrong; here's the real more generic version (we have to prevent the first subgroup match from "running away"):
replace all occurrences of regex '<([^>]+)>([^<]*)</>' in S with '<$1>$2</$1>'.The regular expression works fine for me and is correct; if it doesn't work on your end check if you're using the same line and also check the links I've given for further reference.
‎2010 Nov 04 1:08 AM