I have string a = 'a_b_c_d(1.0)'
I need to extract the number within the bracket.
my answer shoud be b = '1.0'
how can i do this using regular expression or other method?
Thank you

댓글 수: 4

How about this?
regexp(x,'(?<=\().*(?=\))','match')
Thank you
Guillaume
Guillaume 2015년 6월 29일
It may be safer to use a non-greedy * (that is *?) in case there is more than one bracketed expression in the string.
Brian Hannan
Brian Hannan 2015년 6월 29일
Good idea.

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

 채택된 답변

Guillaume
Guillaume 2015년 6월 29일
편집: Guillaume 2015년 6월 29일

1 개 추천

Any number of regular expression would do 'extract whatever is between two brackets:
str = 'a_b_c_d(1.0)';
regexp(str, '(?<=\()[^)]*(?=\))', 'match', 'once')
is one possibility. This
  • looks ahead for an opening bracket.
  • matches any number of characters as long as they're not a closing bracket
  • looks behind for a closing bracket.

추가 답변 (1개)

Thorsten
Thorsten 2015년 6월 29일
편집: Thorsten 2015년 6월 29일

0 개 추천

This extracts all digits and all '.' from a:
numstr = a(regexp(a, '[\d\.]'))
This extracts all numbers between ( ), where number must have at least one digit before the point and one digit after the point:
numstr = regexp(a, '\((\d+\.\d+)\)', 'tokens')
The outer \( and \) match the '(' and ')', resp., in a, and the inner ( ) are meta-characters to group a token.

카테고리

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

질문:

2015년 6월 29일

편집:

2015년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by