regexp question (-.- 4 word minimum)

조회 수: 2 (최근 30일)
Lucas
Lucas 2012년 9월 18일
I have a string thats goes like this:
055451051 (/document name/folder/file name)
I want a simple regexp that will give me the name between the third / to the ). I tried:
regexp(str, '/(?<name>\w*))', 'names')
but it keeps returning a 0x0 struct. I can get it if I use split on the string using '/' as a delimeter. But my file name come out like 'File name)' and has the parentheses on the end. I can get rid of it but I'm thinking that there's an easier way for me to get whats between the / and ).
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2012년 9월 18일
+1 Let's wait until the regexp masters wake up!

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
편집: Azzi Abdelmalek 2012년 9월 18일
s='055451051 (/document name/folder/file name)'
out=s(max(strfind(s,'/'))+1:end-1)
in the above example the third is the last, if they are more then 3 /. then
s='055451051 (/document name/folder/file name)'
idx=strfind(s,'/')
out=s(idx(3)+1:end-1)

추가 답변 (1개)

Matt Tearle
Matt Tearle 2012년 9월 18일
I know you've already got it working, but regexp challenges are always fun. This seems to work...
x = regexp(str,'/([\w\s]*?))','tokens')
Match any number of characters and spaces, but only as many as necessary, between a / and a ).
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2012년 9월 18일
regexp challenges always make me feel dumb and inadequate...
Thanks for posting it!

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

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by