replacing a string with another and vice versa

Hi, I would like to write two functions:
1- replace all occurrences of 'a(number)' and 'bb(number)' with respectively 'a_number' and 'bb_number'. More concretely, a(1) would become a_1 and bb(1285) would become bb_1285. the typical string would look like
string='log(a(2))+a(3)*cosh(exp(bb(5))'
and the result would be
string='log(a_2)+a_3*cos(exp(bb_5))'
2- do the inverse operation
Is there any efficient way of doing this? My sense is that it can be done with regular expressions but I am just a beginner on that front and I would not know how to go about this. Your help will be appreciated.
thanks,
Pat.

댓글 수: 3

Is this at all related to your previous question?
As an answer to your question, yes this is a pretty easy regexp problem. What have you tried so far? How do you detect expressions of the form a(number)?
Patrick Mboma
Patrick Mboma 2012년 10월 3일
편집: Patrick Mboma 2012년 10월 3일
I tried '(a|bb)\(d*\)' but it did not work.
You forgot to escape the "d"

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 10월 3일
편집: Matt Fig 2012년 10월 3일

0 개 추천

str = 'log(a(2))+a(3)*cosh(exp(bb(5)))'; % Initial string
str = regexprep(str,'(bb)\((\d)\)|(a)\((\d)\)','$1_$2')

댓글 수: 4

Hi Matt,
Thank you for the answer. Please teach me regular expressions and/or tell me how to learn and master them. Could you do the inverse operation and retrieve the original string?. If you don't mind, could you tell me what the patterns '(bb)\((\d)\)|(a)\((\d)\)' and '$1_$2' do?
Matt Fig
Matt Fig 2012년 10월 3일
편집: Matt Fig 2012년 10월 3일
Hello Patrick. Everything in parenthesis is captured as a token. The way I set it up there are two tokens, either a or b is the first token, then the number is the second token. These tokens are passed to the replacement string where they are referenced in order by $1, $2, etc. The best I can do is tell you to read the documentation on regular expressions.
Millions thanks Matt!
I also figured out it is possible to simplify things even further by writing
str2 = regexprep(str,'(bb|a)\((\d)\)','$1_$2')
then one can also do the inverse operation
str3 = regexprep(str2,'(bb|a)(\_)(\d)','$1($2)')
Indeed!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

per isakson
per isakson 2012년 10월 3일
편집: per isakson 2012년 10월 4일

1 개 추천

A start of one way to use regular expression:
string = regexprep( string, '(?<=a)\((\d+)\)', '_$1' );
string = regexprep( string, '(?<=bb)\((\d+)\)', '_$1' );
.
-- in one line ---
Look for "(one or more digits)" that comes directly after "bb" or "a"
>> string = regexprep( string, '(?<=((bb)|a))\((\d+)\)', '_$1' )
string =
log(a_2)+a_3*cosh(exp(bb_5)
  • (?<=((bb)|a)) "Look behind from current position and test if expr is found." Where expr evaluate to "a" or "bb".
--- another one-liner ---
>> str = log(a(2))+a(3)*cosh(exp(bb(5));
>> regexprep( str, '((bb)|a)\((\d+)\)', '$1_$2' )
ans =
log(a_2)+a_3*cosh(exp(bb_5)
  • (bb)|a stands for "bb" or "a"
  • (expr) stands for group regular expressions and capture tokens
  • *\(* stands for "("
  • \d+ stands for one ore more digits

댓글 수: 3

Thanks Per. Is it not possible to combine both expressions using some form of "OR"? Also, I am trying to understand what you've written means: the underscore, the dollar sign, the "1"...
per isakson
per isakson 2012년 10월 3일
편집: per isakson 2012년 10월 3일
Yes it is, but why bother? Matt, has done it. With regular expressions one must not make it more complicated than one master.
See above

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by