필터 지우기
필터 지우기

condition in regular expression

조회 수: 3 (최근 30일)
Patrick Mboma
Patrick Mboma 2017년 1월 28일
편집: Patrick Mboma 2017년 1월 28일
Hi,
I would like to match a regular expression, which can take two forms:
aaa1(-1)&abcd
or
bbb2&aefdg
More specifically,
  • the expression starts with word characters (e.g. aaa1 or bbb2)
  • it is optionally followed by a parenthesis, then a minus sign, then a closing parenthesis
  • it is followed by the ampersand (&)
  • it is followed by word characters and then
I would like to capture those expressions and I wrote the following code, which does not work well
expr='\<(?<before>\w+)\>(\(-)?(?<digit>\d+)?(\))?&(?<after>\w+)';
regexp('vvv&mp abvg(-5)&ads abvg-5&ads',expr,'names')
It does not work well because the third expression (abvg-5&ads) should not be a match. That is, the digits should be matched only if they are around parentheses.
I thought that maybe using some form of condition
(?(cond)expr1|expr2)
would help but I was not successful in implementing it. Maybe that is the way to go, maybe there is another way, I don't know.
Any suggestions?
Thanks

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by