MATLAB: What is the use of /begin…../end in regex

MATLAB

I want to know what is the use of /begin and /end in the pattern for a regex.. For eg:
pattern = /begin ONE (\w*) /end ONE;
regexp(text,pattern,'match')
Could someone please explain.

Best Answer

The / character and "begin" and "end" have no special significance in regexp: they are just plain text characters.
Which is to say that the /begin and /end are looking for those literal strings in the text.
That bit of code is looking for the literal characters /begin followed by one space, followed by the literal word ONE followed by one space.
Then the (\w*) means (in this context) that what is matched by the inside of the () is what is to be returned by regexp. What is inside the () is \w* which means a sequence of zero or more "word" characters. "word" characters are any alphabetic characters (including in non-English alphabets), together with the numeric digits 0 through 9, together with underscore -- very similar to the MATLAB identifier rule, except MATLAB identifiers do not permit non-English alphabetic characters.
After the "word", the expression expects one space and then the literal characters /end ONE
examples of valid input:
/begin ONE strawberry_icecream /end ONE
/begin ONE /end ONE
/begin ONE 23andMe /end ONE
Examples of input that would not get matched:
/begin ONE strawberry-icecream /end ONE
/begin ONE 23andMe ONE /end
/begin ONE strawberry icecream /end ONE