Recovering text between parentheses using regexp

Hi,
I am trying to create a regular expression using which i can recover the string between two parenthesis. I have created the following expression
exp='[^(]+.*[^)]+';
matchStr = regexp(str,exp,'match');
This expression works fine if the string is off any one of the formats mentioned below:
a) str = '(1r)';
b) str = '(0.5;0.5;'rrr')'; etc
but does not work if the string is off the form
str = '(r)'; or str = '(1)';
i.e. it doesn't pick up string which are just off 1 character between the parenthesis. I think i am missing something very basic here. Is it that regex will not work for single characters and will only work for strings?
Thanks in advance,
Regards,
Giri

댓글 수: 3

Rik
Rik 2018년 5월 7일
You can look for a solution in the question by someone with the exact same question here.
Giri
Giri 2018년 5월 7일
Thanks Rik..

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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2018년 5월 7일

2 개 추천

exp = '[^()]*'; should be it.

댓글 수: 2

Giri
Giri 2018년 5월 7일
Thanks Fangjun for that.. it works fine... Can you please explain how this works? Is it like, it just ignores ( and ) in the text and takes everything else?
doc regexp
[^c1c2c3]: Any character not contained within the brackets.
expr*: 0 or more times consecutively.

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

추가 답변 (0개)

카테고리

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

태그

질문:

2018년 5월 7일

댓글:

2018년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by