Simplify a string with MATLAB script
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello !
I have a string format which looks like this (this is not always 'A' before '_' and numbers) :
Eq = 'A_1+((A_2+A_3)&(A_4+A_5))+A_6';
How can i simplify the string like this (with a script) :
Eq = 'A_(1+((2+3)&(4+5))+6)'
For me the simplest way would be to delete all 'A_' iteration except the first one and to add a '(' after the first 'A_' but i don't know how to do it in a script.
Thank you in advance for your help !
Edit : After Stephen answer review, i realized i missed a detail : sometimes the string can be like this :
'A_number_1+((A_number_2+A_number_3)&(A_number_+A_number_5))+A_number_6';
==> 'A_number_(1+((2+3)&(4+5))+6)'
With multiple string for example : 'String_String_String_1' . Is there anyway to delete string and '_' behind a number ?
채택된 답변
>> Eq = 'A_1+((A_2+A_3)&(A_4+A_5))+A_6';
>> Fq = regexprep(Eq, '^([A-Z]+_)(.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
A_(1+((2+3)&(4+5))+6)
댓글 수: 10
Lucas S
2020년 2월 27일
Thanks ! I really need to understand how to use regexp, i clearly didn't understand how you did this.
And if for example the string is more like that :
Eq = 'A_number_1+((A_number_2+A_number_3)&(A_number_+A_number_5))+A_number_6';
==> 'A_number_(1+((2+3)&(4+5))+6)'
Is there a way to delete all string before a '_' ? Because with this script it only simplify 1st string which is 'A_' here
"Is there a way to delete all string before a '_' ?"
Of course, you just need to specify exactly what you require.
"Because with this script it only simplify 1st string which is 'A_' here"
Because that is exactly what you specified in your question.
So, making a few guesses... perhaps something like this does what you want (I added the missing '4'):
>> Eq = 'A_number_1+((A_number_2+A_number_3)&(A_number_4+A_number_5))+A_number_6';
>> Fq = regexprep(Eq, '^(\w+?)(\d+\W.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
A_number_(1+((2+3)&(4+5))+6)
An example where I replaced "number" with some random digits:
>> Eq = 'A_123_1+((A_123_2+A_123_3)&(A_123_4+A_123_5))+A_123_6';
>> Fq = regexprep(Eq, '^(\w+?)(\d+\W.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
A_123_(1+((2+3)&(4+5))+6)
And with the original example from your question:
>> Eq = 'A_1+((A_2+A_3)&(A_4+A_5))+A_6';
>> Fq = regexprep(Eq, '^(\w+?)(\d+\W.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
A_(1+((2+3)&(4+5))+6)
How the regular expression is defined:
'^(\w+?)(\d+\W.*)'
%^ match start of string
% ( ) token 1
% \w+? lazy match of letters, digits, and underscore
% ( ) token 2
% \d+ greedy match of digits
% \W match one non-letter, non-digit, non-underscore (e.g. plus, parenthesis, etc.)
% .* greedy match of any characters
Token 1 matches the 'String_String_String_' part of the name (or whatever you call it), but lazily (i.e. as few characters as possible), while token 2 greedily matches the digits, e.g. '1' (i.e. it tries to collect as many digits as possible). The \W forces the digits at the start of token 2 to be the last digit/s in the name (or whatever you call it), even if digits occur elsewhere in token 1. The replacement string works like this:
'$1\(${strrep($2,$1,'''')}\)'
%$1 token 1, e.g. 'String_String_'
% \( \) literal parentheses
% ${ } dynamic expression to call function
% strrep($2,$1,'''') all instances of token 1 in token 2 replaced with ''
Thank you very much ! I think i understand how you did it so i can adapt if my string has more 'string_' iterations
Lucas S
2020년 2월 28일
Ok i think with regex i can't solve my problem as my string has never the same format.
Stephen23
2020년 2월 28일
"I think i understand how you did it so i can adapt if my string has more 'string_' iterations "
You certainly do NOT need to "adapt" the regular expression I gave in my previous comment for more "'string_' iterations", because that regular expression does not know or or care about "iterations" within the name. It only matters that the name contains one or more letters, digits, and/or underscores. What order those characters are in is totally irrelevant, nor is the fact that a human might see "iterations" within the name.
"Ok i think with regex i can't solve my problem as my string has never the same format."
What "format" are you referring to? The "format" of the entire string or just the leading name?
Rather than jumping to incorrect conclusions based on incorrect understandings of regular expressions, I recommend communicating the exact "format" requirements that you have, together with some examples.
In the future i would insert this script in a loop with many different string.
The strings have the same format :
'string_string_string_number'
But the lengths can be different :
it can be 'string_string_number'
or 'string_string_string_number'
or 'string_string_string_string_number'
etc...
I found a solution that can work : Store one ieration of the full string (before the number) delete all of it and add it again at the start.
Or use regexp but for me regexp is only for same format's same length's string (i'm probably wrong)
I made this and i think it's working (ugly but working) :
A = 'A_number_1+((A_number_2+A_number_3)&(A_number_4+A_number_5))+A_number_6';
CR_string = '';
for i=1:length(A)
if A(i) == '('
else
if str2double(A(i)) == 1
break;
else
CR_string = strcat(CR_string, A(i));
end
end
end
A = erase(A, CR_string);
A = strcat(CR_string, '(', A, ')');
disp(A);
"'Or use regexp but for me regexp is only for same format's same length's string (i'm probably wrong)"
I can't see anything in the regular expression that restricts it to "same length's string" as you write, it adapts exaclty to the the length of the leading name, with any number of 'string_' repetitions.
When I run it on all of your example strings, I get the expected outputs, e.g.:
>> Eq = 'A_number_1+((A_number_2+A_number_3)&(A_number_4+A_number_5))+A_number_6';
>> Fq = regexprep(Eq, '^(\w+?)(\d+\W.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
A_number_(1+((2+3)&(4+5))+6)
and
>> Eq = 'string_string_number_1+((string_string_number_2+string_string_number_3)&(string_string_number_4+string_string_number_5))+string_string_number_6';
>> Fq = regexprep(Eq, '^(\w+?)(\d+\W.*)', '$1\(${strrep($2,$1,'''')}\)')
Fq =
string_string_number_(1+((2+3)&(4+5))+6)
etc. etc.
Please show me a single example of my code that does not give the expected output, so that I can check it.
Lucas S
2020년 2월 28일
Hmmm ok i'm dumb i tried with your 1st post thinking it was the new one ... My bad thank you !
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
