removing parentheses around digits using regular expressions
Mostrar comentarios más antiguos
Dear all, I am slowly making progress on my learning of regular expressions. At the moment, I am trying to solve the following problem: replace all occurrences of (n) with n, where n is a number, provided that no alphabetical letter occurs before the first parenthesis. As an example,
str='(2)+p_5*(3)-(0.3)'
would become
2+p_5*3-0.3
I wrote the following
regexprep(str,'(\W)(\()([.012345789]+)(\))','$1$3')
but it does not solves the problem if one of the expressions to change occurs at the beginning as in the example above. More concretely, the answer I get from running this is
(2)+p_5*3-0.3
which is not the expected result.
Thanks in advance for any help
Pat.
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 5 de Oct. de 2012
0 votos
Consider using a "look-behind"
per isakson
el 5 de Oct. de 2012
Editada: per isakson
el 5 de Oct. de 2012
>> regexprep( str, '\(([\d.]+)\)', '$1' )
ans =
2+p_5*3-0.3
str =
(2)+p_5*(3)-(0.3)+exp(3)
>> regexprep( str, '\(([\d.]+)\)', '$1' )
ans =
2+p_5*3-0.3+exp3
- *\(* represents "("
- \) represents ")"
- [\d.]+ represents one or more digits and periods, e.g. "....0" and "2"
- (expr) "Group regular expressions and capture tokens." The token may be refered to in the replacement string by $1 - "1" because it is the first
Every substring in the string that matches this expression is replaced, i.e. a number enclosed by parentheses is replaced by the number.
.
--- in response to a comment ---
>> regexprep( str, '(?<!\w)\(([\d.]+)\)', '$1' )
ans =
2+p_5*3-0.3+exp(3)
better
>> regexprep( str, '(?<![a-zA-Z])\(([\d.]+)\)', '$1' )
because \w includes digits.
- (?<![a-zA-Z]) "Look behind from current position and test if expr is not found." Where expr evaluates to a letter. Thus, if preceded by a letter there is no match.
7 comentarios
Patrick Mboma
el 5 de Oct. de 2012
per isakson
el 5 de Oct. de 2012
Editada: per isakson
el 5 de Oct. de 2012
Yes, it does. What should it give? OK preceded by letter
per isakson
el 5 de Oct. de 2012
What about atan2()?
Matt Fig
el 5 de Oct. de 2012
The solution I posted in the comments to my answer handles atan2.
str='(2)+p_5*(3)-(0.3)+cos(5.7)+(3)^(2-3) + (.55) + atan2(9)';
regexprep(str,'(?<![\w])(\()([\d*\.]+)(\))','$2')
ans =
2+p_5*3-0.3+cos(5.7)+3^(2-3) + .55 + atan2(9)
per isakson
el 5 de Oct. de 2012
Editada: per isakson
el 5 de Oct. de 2012
Yes, and that is because "\w" stands for "[A-Za-z0-9]". However, it is difficult to know whether including "0-9" might have any unintended side effects.
I find it difficult to construct robust expressions. However, I have never tried to learn regular expressions in a systematic way.
Patrick Mboma
el 5 de Oct. de 2012
per isakson
el 6 de Oct. de 2012
Editada: per isakson
el 6 de Oct. de 2012
Categorías
Más información sobre Programmatic Model Editing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!