get char between two specified characters

2 次查看(过去 30 天)
I have a string like '[Hello]'. Now I want to get only the text between the brackets []. The result should be 'Hello'. When the string is '(Hello)' the result should be empty.
I tried with regexp but was not successfully.
regexp('[Hello]', '(\w)')
ans = 2 3 4 5 6
But I get the same result with normal brackets in the string
regexp('(Hello)', '(\w)')
ans = 2 3 4 5 6
Can anyone help me?

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-3-1
编辑:Azzi Abdelmalek 2013-3-1
p='[Hello\]'
s={'[Hello]','(Hello)','[Hello]erz'}
r=regexp(s,p,'match')
r=r(cellfun(@(x) ~isempty(x),r))
out=cellfun(@(x) x{1}(2:end-1),r,'un',0)

更多回答(0 个)

类别

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