Is it possible to use regexprep to insert a '*' between a number following a letter?

4 ビュー (過去 30 日間)
I have a an excel file containing cells of letters and number. I want to add a * symbol every time a number comes directly after a letter.
Some examples of the individual cells are,
28Si+H3
13C+D3
23Na
C2+H4
13C2+H2
So I want search each one and if a number follows a letter to put * in between.
28Si+H3 would become 28Si+H*3
  3 件のコメント
Stephan
Stephan 2018 年 8 月 30 日
@Rik Because number follows letter
Image Analyst
Image Analyst 2018 年 8 月 30 日
編集済み: Image Analyst 2018 年 8 月 30 日
Because 28 is not letters A-Z. And, even though it's after 28, Si is not a number. If it were Si28, it would become Si*28, but it's not so you leave it alone.

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

採用された回答

Rik
Rik 2018 年 8 月 30 日
Now I'm awake again: you can also do this in a oneliner:
str='C2+H4-28D';
m=regexprep(str,'([A-Za-z])(\d)','$1*$2');
  4 件のコメント
Rik
Rik 2018 年 8 月 30 日
@Stephen, you're welcome. I still struggle with regular expressions sometimes, but the best way to learn them is to just start. I still grab the doc for regexp whenever I'm writing a regexp, even simple ones.
PS if this solved your question, please consider marking it as accepted answer
@jonas, your post gave me something I could easily adapt. As someone once told me: a good programmer is lazy, i.e. borrow and tweak working code whenever you can ;)
stephen geddes
stephen geddes 2018 年 8 月 30 日
Haven't posted any questions until today. Didn't know I should do that! Thanks again.

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

その他の回答 (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