필터 지우기
필터 지우기

What is the function of a question mark '?' in regular expression

조회 수: 15 (최근 30일)
Rui Zhang
Rui Zhang 2019년 8월 1일
댓글: madhan ravi 2019년 8월 2일
If I have a C code text with two comments like below and I want to use matlab regular expression function to remove all the comments in the C file called "myTextFile.txt"
/* This is my first comment */
void myCfun(void){}
/* This is my second comment */
Below is my first matlab script, which does not work well:
mytext = fileread('myTextFile.txt');
searchPattern = '/\*.*\*/';
matchedString = regexp(mytext,searchPattern,'match');
The result is:
matchedString = {' This is my first comments */void myCfun(void){}/* This is my second comment */'};
In the C file there should be two comments, however, without a question mark, the matlab regexp function include all the text between the first '/*' and the last '*/'. This is not what I want.
Then I modifed the search pattern as the following:
mytext = fileread('myTextFile.txt');
searchPattern = '/\*.*?\*/';
matchedString = regexp(mytext,searchPattern,'match');
This time it gives the right answer:
matchedString = {' This is my first comments', ' This is my second comment'};
I don't understand what the question mark does in the above example?
  댓글 수: 1
madhan ravi
madhan ravi 2019년 8월 2일
madhan ravi:
Rui Zhang:
I went to the website where you provided the link to.
My understanding is the question mark '?' is equivalent to {0, 1}. But that doesn't help me on my question.
In my examples, it looks like the question mark changes the search direction. Without it, the function seaches the given string from the last character; with it, it searchs the string from the first character. This is my perception that comes from my example above.

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

채택된 답변

Adam Danz
Adam Danz 2019년 8월 1일
편집: Adam Danz 2019년 8월 1일
Let's interpret the 1st searchPattern.
searchPattern = '/\*.*\*/';
% /\* start with /*
% .* allow for any character(s) (Greedy!)
% \*/ keep searching until you get to the LAST */
This matches all of your text because your text begins with /* and ends with */. It doesn't care that there's another */ before the last one. This is known as a greedy search.
Let's interpret the 2nd searchPattern
searchPattern = '/\*.*?\*/';
% .*? allow for any character(s) until you get to....
% \*/ ....the first time this occurs
This match ends at the next */ unlike the above match that ends at the last */.
  댓글 수: 6
Adam Danz
Adam Danz 2019년 8월 1일
@Walter , great example. Thank you!
madhan ravi
madhan ravi 2019년 8월 2일
+1, definitely it takes sometime to learn regex without a doubt as Adam said.

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

추가 답변 (1개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by