How to discard all punctuation from a text file
Ältere Kommentare anzeigen
Hello, I need a MATLAB code to discard all punctuation and signs from a text file.I want to keep only characters and numbers.Thanks.
Akzeptierte Antwort
Weitere Antworten (3)
Star Strider
am 18 Jan. 2016
Bearbeitet: Star Strider
am 19 Jan. 2016
One possible approach:
str = 'Hello, I need 1 MATLAB code to discard all punctuation, and signs from 9 text files.';
Idx = regexp(str, '[^. , !]');
Result = str(Idx)
EDIT — To keep the spaces, just remove them from the regexp pattern string (in this instance, I was telling it to exclude spaces as well as the punctuation):
str = 'Hello, I need 1 MATLAB code to discard all punctuation, and signs from 9 text files.';
Idx = regexp(str, '[^.,!]');
Result = str(Idx)
Result =
Hello I need 1 MATLAB code to discard all punctuation and signs from 9 text files
You can add as many other punctuation or other characters as necessary between the square brackets ‘[]’, depending on what appears in your strings that you do not want in the result.
2 Kommentare
Mubashir
am 16 Sep. 2017
I want to use this in table. I can't remove punctuation's in whole column.
Image Analyst
am 16 Sep. 2017
So then go down the table's column one row at a time. What's wrong with that? It's easy.
Walter Roberson
am 19 Jan. 2016
1 Stimme
https://www.mathworks.com/matlabcentral/newsreader/view_thread/127125
2 Kommentare
Fateme Jalali
am 19 Jan. 2016
Walter Roberson
am 19 Jan. 2016
YourString = [' ' YourString ' '];
Image Analyst
am 19 Jan. 2016
Yet another way:
str = 'Hello, ~!@#$^&*()_+.,<>;"?I need 1 MATLAB code to discard all punctuation, and signs from 9 text files.'
% Get logical index to keep space, numbers, and upper and lower case letters.
keeperIndexes = str == ' ' | (str>='0' & str<='9') | ...
(str>='a' & str<='z') | (str>='A' & str<='Z');
strOut = str(keeperIndexes) % Extract only those elements
Kategorien
Mehr zu Simulation and Analysis finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!