Change digits in strings
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I have a problem. How I can change digits in string for example:
"A[12,12]*A[12,12]+A[9,12]*A[12,9]+A[5,2]*A[1,3]"
to have
"A[11,11]*A[11,11]+A[8,11]*A[11,8]+A[4,1]*A[0,]"
using regexprep ?
Best
採用された回答
Guillaume
2016 年 5 月 10 日
You need to use a dynamic replacement string
str = '"A[12,12]*A[12,12]+A[9,12]*A[12,9]+A[5,2]*A[1,3]"';
regexprep(str, '\d+', '${num2str(str2double($0)-1)}')
8 件のコメント
Guillaume
2016 年 5 月 11 日
Mariusz's comment posted as an answer moved here:
Hello,
Thank you for your help.
I have one problem. For example:
Temp1 := 'A[1,1]*A[1,1]+A[1,2]'
Temp2 := 'A[1,1]*A[1,1]+A[1,2]'
How to split string by '='?. When I use regexp I have
' Temp1 ' [1x45 char] [1x45 char]
but I wont to have
' Temp1 ' [1x45 char] ' Temp1 ' [1x45 char]
Best
Guillaume
2016 年 5 月 11 日
Please use comments rather than starting a new answer.
I'm confused as to how this relates to the previous question? Do you still want to decrease the numbers in the string? If this is unrelated, you should accept my answer and start a new question.
Additionally, it's not clear what your input is. Is it a 2D char array? a cell array of strings? something else?
The output of regexp is totally dependent on what regular expression you use, so if you say that a regular expression does not work, please show that regular expression.
Mariusz
2016 年 5 月 12 日
Hello,
I do not want to decrease the numbers in the string. The input is string. To split string I use
STR =
K[0,0] := A[1,1]+b[1,1]; %(here is new line - '\n')
K[0,1] := A[1,2]+b[1,2]; %(here is new line - '\n')
K[1,0] := A[2,1]+b[2,1]; %(here is new line - '\n')
K[1,1] := A[2,2]+b[2,2]; %(here is new line - '\n')
regexp(STR,':=','split')
The result is
' K[0,0] ' [1x25 char] [1x25 char] [1x25 char] ' A[2,2]+b[2,2];'
Best, Mariusz
Guillaume
2016 年 5 月 12 日
The output you get makes sense. regexp does not care about line ending characters, as far as it is concerned your input is a single string and you ask it to split it at the :=, so the second portion of the string is ' A[1,1]+b[1,1]; \nK[0,1] '. What you really want is to split the string at the := and at the newline:
STR = sprintf('%s\n%s\n%s\n%s\n', ...
'K[0,0] := A[1,1]+b[1,1]; ', ...
'K[0,1] := A[1,2]+b[1,2]; ', ...
'K[1,0] := A[2,1]+b[2,1]; ', ...
'K[1,1] := A[2,2]+b[2,2]; ')
regexp(STR, ':=|\n', 'split')
%or if you want to trim the strings at the same time
regexp(STR, '\s*(:=|\n)\s*', 'split')
Mariusz
2016 年 5 月 13 日
Hello,
Thank you for your help. When I test my code I found one problem in the decrease the numbers in the string.
When I have string:
'A[1,2]*2.0233+x[856]'
I use
regexprep('A[1,2]*2.0233+x[856]', '\d+', '${num2str(str2double($0)-1)}')
the results is
'A[0,1]*1.232+x[855]'
but should be
A[0,1]*2.0233+x[855]
How solve this problem?
Best,
Mariusz
The original regular expression would match any continuous sequence of digits and decrease that sequence by 1. If you want restrictions on that, it's up to you to specify these restrictions in the regex. Your question is too open ended for me to know what these are. They could be:
- only match digits immediately following an opening square bracket or immediately preceding a closing square bracket:
regexprep(s, '(?<=\[)\d+|\d+(?=\])', '${num2str(str2double($0)-1)}')
- only match digits preceded or followed by anything but '.', which would restrict it to integers as long as you don't use e notation. One possible downside of this is that the digits can't be at the beginning or end of the string
regexprep(s, '(?<=[^.])\d+(?=[^.])', '${num2str(str2double($0)-1)}')
- something else
Note that it is difficult in regular expression to say to not match something
Mariusz
2016 年 5 月 23 日
Hello,
Thank you for help.
Best
Mariusz
arthur doroshev
2020 年 12 月 15 日
and if you want to get from
'A[12,12]*A[12,12]+A[9,12]*A[12,9]+A[5,2]*A[1,3]'
that
'A[1]*A[2]+A[3]*A[4]+A[5]*A[6]'
what to do then?
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
