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

REGEX help needed

Former Member
0 Likes
675

Hi Experts.

I have a text (from Read_text) which comes like this : ....

 <H>... &lt;/&gt; ....<T>...&lt;/&gt;

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
642

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)...

4 REPLIES 4
Read only

Former Member
0 Likes
643

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)...

Read only

0 Likes
642

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?

Read only

0 Likes
642

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.

Read only

0 Likes
642

Bang on Harald. Works perfectly!

Thanks a ton.