필터 지우기
필터 지우기

removing parentheses around digits using regular expressions

조회 수: 4 (최근 30일)
Patrick Mboma
Patrick Mboma 2012년 10월 5일
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.

채택된 답변

Matt Fig
Matt Fig 2012년 10월 5일
편집: Matt Fig 2012년 10월 5일
regexprep(str,'(\()([\d*\.]+)(\))','$2')
  댓글 수: 10
Walter Roberson
Walter Roberson 2012년 10월 5일
For example, 1+()*2 the empty string inside the () would match because all parts of \d*\.?\d* are optional, so the expression would be converted to 1+*2
Also, I note that the problem description does not disallow numeric characters before the (), so 1+Henkel2(7)*5 would be converted to 1+Henkel27*5
per isakson
per isakson 2012년 10월 5일
편집: per isakson 2012년 10월 6일
Yes.
Assuming the requirement is to match numbers only. The second expression below seems to be closer to a working one. However, what expression is taught in the book?
>> regexprep( '(12),(.12),(12.),(1.2),(.),()' ...
, '\((\d*\.?\d*)\)', '#$1' )
ans =
#12,#.12,#12.,#1.2,#.,#
>> regexprep( '(12),(.12),(12.),(1.2),(.),()' ...
, '\(((\d+\.?\d*)|(\d*\.\d+))\)', '#$1' )
ans =
#12,#.12,#12.,#1.2,(.),()
.
\w takes care of the case with Henkel2 - by intent or not. However, are there any cases, in which "(" should be replaced when preceded by a digit?

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 10월 5일
Consider using a "look-behind"
  댓글 수: 2
Patrick Mboma
Patrick Mboma 2012년 10월 5일
편집: Patrick Mboma 2012년 10월 5일
Hi Walter,
My knowledge of regular expressions is not that good yet... I don't know yet how to implement "look behinds".

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


per isakson
per isakson 2012년 10월 5일
편집: per isakson 2012년 10월 5일
>> 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
Patrick Mboma
Patrick Mboma 2012년 10월 5일
I am currently doing just that, with you guys' help.
Thanks to everybody.
Pat.

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

카테고리

Help CenterFile 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