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
Guillaume 2016 年 5 月 10 日

0 投票

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
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
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
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
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
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
Guillaume
Guillaume 2016 年 5 月 13 日
編集済み: Guillaume 2016 年 5 月 13 日
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
Mariusz 2016 年 5 月 23 日
Hello,
Thank you for help.
Best
Mariusz
arthur doroshev
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 ExchangeCharacters and Strings についてさらに検索

質問済み:

2016 年 5 月 10 日

コメント済み:

2020 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by