Parsing everything between quotation using regular expression

I m working on a code that parse whatever occur between the quations as shown in my example below
raw="square format" tag_id=
I want parse only between the quotations (highlighted in bold) "whateverhere even space or dots" parse me
your help is highly appreciated

댓글 수: 2

  • What has tag_id= to do with the question?
  • raw="square format" tag_id= is that a sample string of text, out of which you want to extract square format?
You say "whateverhere even spaces or dots". Does that include the possibility of double-quotes ?

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

 채택된 답변

Ken Atwell
Ken Atwell 2014년 2월 7일
I think you want:
raw='"square format" tag_id='
regexp(raw, '"(.*?)"', 'tokens')
That gives you a cell array of all text found within double quotes, and nothing else. KEN

댓글 수: 5

sst
sst 2019년 3월 20일
편집: sst 2019년 3월 20일
Hello Ken, by any chance do you have one for a slight variation of this problem? I was wondering if there's a good way to exclude the escape sequence \" directly in the regular expression pattern.
raw = ' "This is a sample string \" with an escape sequence" 0 1 abcdef';
regexp(raw, '"(.*?)(<!\\)"', 'tokens')
However, this will fail if the \ is itself escaped. For example in
' "This is a sample string \\" with an escaped escape sequence" 0 1 abcdef'
then that is not really an \" since the \\ is marked as literal.
Parsing through to check for odd numbers of \ is a bit more complicated.
Hello Walter, unfortunately that didn't work (R2018b). I only need to include the \" escape sequence, to avoid the ambiguity involved in the use of double quotes.
>> raw = ' "This is a sample string \" with an escape sequence" 0 1 abcdef';
>> regexp(raw, '"(.*?)(<!\\)"', 'tokens')
ans =
0×0 empty cell array
sst, please start a fresh question rather than commenting on a 5 year old answer.
regexp(raw, '"(.*?)(?<!\\)"', 'tokens')
You might want to add the 'once' option if you only expect one occurance.

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

추가 답변 (0개)

카테고리

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

질문:

2014년 2월 7일

댓글:

2019년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by