我不知道为什么您可能需要这样做,但是有一种方法可以让您区分除第四个匹配表达式以外的所有情况下的“no match”和“empty string match”条件
'|b'
. 这个表达式似乎试图匹配“没有或没有”
'b'
“,我认为这和你以前的表达没有什么不同
'b?'
,这是“
“b”
0次或1次”。
如果你加上
beginning and end anchors
'emptymatch'
option
对于
regexp
,那么你会得到一个空的牢房
{}
对于不匹配项和包含空字符串的单元格
{''}
对于空字符串匹配:
expr1 = '^b$';
regexp('a', expr1, 'match', 'emptymatch') % Result = {}
regexp('b', expr1, 'match', 'emptymatch') % Result = {'b'}
regexp('', expr1, 'match', 'emptymatch') % Result = {}
expr2 = '^$';
regexp('a', expr2, 'match', 'emptymatch') % Result = {}
regexp('', expr2, 'match', 'emptymatch') % Result = {''}
expr3 = '^b?$';
regexp('a', expr3, 'match', 'emptymatch') % Result = {}
regexp('b', expr3, 'match', 'emptymatch') % Result = {'b'}
regexp('', expr3, 'match', 'emptymatch') % Result = {''}
expr5 = '^.*$';
regexp('b', expr5, 'match', 'emptymatch') % Result = {'b'}
regexp('', expr5, 'match', 'emptymatch') % Result = {''}