フィルターのクリア

how to use regular expression to remove a prefix string with all 0s.

16 ビュー (過去 30 日間)
liangjian
liangjian 2011 年 12 月 2 日
コメント済み: Walter Roberson 2015 年 2 月 28 日
I have a set of strings, some of them are prefixed with 0, and others are not. For instance, there have five strings:
012AB AB0 00230 0045 abc
I want to remove all of the 0s if they are the prefix of a string. If a string does not have 0s at the prefix position, then the string will be kept the same.
Therefore, the above strings should be transferred into
12AB AB0 230 45 abc
How to implement the above functionality using regular expression?

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 12 月 2 日
s ='012AB AB0 00230 0045 abc';
out = regexprep(s,'\<0*','')
  3 件のコメント
Walter Roberson
Walter Roberson 2011 年 12 月 2 日
\< means "beginning of a word" http://www.mathworks.com/help/techdoc/matlab_prog/f0-42649.html#f0-42983
0* means zero or more occurrences of the character '0'
Better would be
\<0+
which would require 1 or more 0's -- there is no point going through the replacement part of the code if there was not at least one 0.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by