- 'John' was replaced by 'ho'
- 'got' was replaced by 'o'
- 'an' was replaced by ''
- A is not a match, not replaced
- 'in' was replaced by ''
- 'math' was replaced by 'ta'
A question on regexprep
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Edward Huang
am 19 Aug. 2019
Kommentiert: Edward Huang
am 19 Aug. 2019
This is a question stem from a Cody Problem when I looked through those efficient solutions. Let's say I have the following code:
s = 'John got an A in math.';
regexprep(s,'\<(\w)(\w*)(\w)\>','$1${$2(end:-1:1)}$3');
If I try to replace each match with only one of its tokens:
regexprep(s,'\<(\w)(\w*)(\w)\>','$1'); % or '$2','$3'
I will find that 'A' can be recognized as any one of them, and yet when it is replaced by '$1${$2(end:-1:1)}$3', only one copy of 'A' is returned. Why?
Thank you in advance.
0 Kommentare
Akzeptierte Antwort
TADA
am 19 Aug. 2019
A is not a match for your regexp,
match = regexp(s,'\<(\w)(\w*)(\w)\>', 'match')
match =
1×5 cell array
{'John'} {'got'} {'an'} {'in'} {'math'}
so it's not replaced by anything,
s2 = regexprep(s,'\<(\w)(\w*)(\w)\>','${$2(end:-1:1)}')
s2 =
'ho o A ta.'
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!