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

 Akzeptierte Antwort

Guillaume
Guillaume am 10 Mai 2016

0 Stimmen

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 Kommentare

Guillaume
Guillaume am 11 Mai 2016
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 am 11 Mai 2016
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 am 12 Mai 2016
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 am 12 Mai 2016
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 am 13 Mai 2016
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 am 13 Mai 2016
Bearbeitet: Guillaume am 13 Mai 2016
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 am 23 Mai 2016
Hello,
Thank you for help.
Best
Mariusz
arthur doroshev
arthur doroshev am 15 Dez. 2020
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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by