Modify Strings in cell array

15 次查看(过去 30 天)
Hi,
I have a cell array:
b = { 'a/b/c/a'; 'k/b' ; 'c/c/d' }
I want to remove the string before the first slash.
The result should be:
b = { 'b/c/a'; 'b' ; 'c/d' }
How can I achieve the result?
  1 个评论
Kevin Phung
Kevin Phung 2019-4-3
编辑:Kevin Phung 2019-4-3
is this the same as removing just the first 2 characters?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-4-4
regexprep(b,'[^/]*/', '', 'once')
  3 个评论
Walter Roberson
Walter Roberson 2019-4-4
Any search always starts with the first character, and we also want to start removing from the first character, so provided we do it right, we do not need to start the pattern with the '^' line anchor (though it would not hurt to do so.)
[list] matches any one character that belongs to the specified list. [^list] matches any one character that does not belong to the specified list. So [^/] matches any one character that is not a slash.
pattern* matches the pattern any number of times, including potentially 0. By default, * is "greedy", and will match the pattern as many times as possible -- basically right until the end of the string if possible, after which the match would start backing up as little as possible at each point to match the remaining things in the pattern. So [^/]* will match as many characters as possible as long as they are not / characters. In multi-line contexts, [^/]* would even advance through multiple rows to look for a / . Notice that newline is not (^) a / character, so [^/]* would even match newline characters if they were there, going as far as possible in the string until it encountered either end of the string or a / character.
You can change this "greedy" property by coding *? instead of * . The ? modifier says to match as few copies of the pattern as necessary in order to match what follows in the pattern. If we were doing multi-line work then it would be a good idea for us to code [^/]*? instead of [^/]* but since we are not working multi-line we can get away with just [^/]*
After that in the pattern is / . In combination with the [^/]*/ you would be matching as many non-slash characters as you could, followed by a / . The / is mandatory for the match to be considered to exist: if the input were 'bear' with no slash then the / that is in pattern would say that there was no match there, whereas if the input were 'bear30a9dfJSE:{^%&@#$)}/' then the pattern would be be happy to consume through all of those non-/ characters until the / was reached. It is important for this purpose that the / is present and is part of the match itself. We did not ask to match only the non-slash characters: we asked to match the non-slash characters and the / that follows. The match for 'bear/abc' is not the 'bear' part: it is the 'bear/' part with the / .
So we started from the beginning of the string, and we matched as many non-slash characters as possible, and we match the / itself as part of the match.
Now... we do a replacement, because this is the Regular Expression Replace (regexprep) function, not the Regular Expression Match (regexp) function.
What we ask to replace with is '' -- the empty string. So we are taking that 'bear/' match out of 'bear/abc', and we replace it with nothingness. Another way of writing that is that we delete the match that includes the slash . 'bear/abc' would become 'abc' because the / was part of the match. If our pattern had just been '[^/]*' then that pattern would have matched the 'bear' part of 'bear/abc' and the replacement with nothingness (deletion) would have left us with '/abc' .
The final thing in the regexprep() call is the 'once' option, so we stop after we have processed once. That stops us from accidently converting 'a/b/c/a' to just the final 'a'
madhan ravi
madhan ravi 2019-4-4
Thank you sir Walter, that was an excellent piece.

请先登录,再进行评论。

更多回答(1 个)

Olaf Oelsner
Olaf Oelsner 2019-4-4
Thank you for solution and explanation!

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by