Change digits in strings
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
