get char between two specified characters

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 1 Mar 2013
Modificato: Azzi Abdelmalek il 1 Mar 2013
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)

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by