dimanche 1 mars 2015

Matlab regex - replace substring ONLY within angled brackets

I would like to find a way to find a way to replace the character _ inside string s with itself surrounded by spaces. The only difficult part is that I'm trying to replace the substring only if the substring is found inside angled brackets. For example (spaces are intentional):


s= 'the quick <_brown _little_fox >, jumped over_the_fence .


wantedresult = the quick < _ brown _ little _ fox>, jumped over_the_fence.


If there is already space on either side, getting extra spaces is perfectly fine.


I tried the following but to no luck: regexprep(s, <[\w ]+(\_)[\w ]+>', ' $1 ');


I think I understand the reason why the above doesn't work, but considering that more than one _ may be inside brackets, I'm not sure how to proceed.


Any advice would be appreciated. Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire