Extract the number within the bracket

21 次查看(过去 30 天)
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 个评论
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.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2015-6-29
编辑:Guillaume 2015-6-29
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
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by