I'm looking to search a String for a given value and then, if this value is found (anywhere in the string), I want to replace the entire string with a new one. Is this possible with regexprep?
Ältere Kommentare anzeigen
As previously stated, I'm looking to search a string for a given value. If this value is present in the string, anywhere at all, I want to replace this entire string with an new one. I've spent about an hour trying to accomplish this with regexprep, but regexprep only seems to allow me to replace the portion of the string I'm searching for with a new string.
For example, I have a variable with a string value of the alphabet here:
String = abcde+fghijklm+nopq+rstuvwxyz
I want to search this string and make sure there aren't any random '+' signs in there to throw off my groove. If there are, I want to replace the entire string with the actual alphabet, without the random plus signs in there. I can search this string for a '+' using regexprep like this
String= regexprep(String,'+','abcdefghijklmnopqrstuvwxyz')
but this method will replace only the plus sign with the alphabet, leaving me with an alphabet inside an alphabet. I want it to replace the entire incorret string with a the new, correct one. Is there any way to do this using regexprep? Or any other method?
My end goal is to search a string for a given value, and then, if found, replace the entire string with a new one. I appreciate any help.
2 Kommentare
Azzi Abdelmalek
am 15 Jul. 2015
Why an example without a '+' in it?
Samuel
am 16 Jul. 2015
Akzeptierte Antwort
Weitere Antworten (1)
Azzi Abdelmalek
am 15 Jul. 2015
Bearbeitet: Azzi Abdelmalek
am 15 Jul. 2015
What you want is to replace a '+' by a blank
String = 'abcdefghijklmnop+qrstuvwxyz'
String= regexprep(String,'+','')
or
String= strrep(String,'+','')
2 Kommentare
Azzi Abdelmalek
am 16 Jul. 2015
Samuel, please edit your original question, try to be brief and clear. You can illustrate with an example, post the expected result.
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!