检查以下内容:
-
\b(?:0?[1-9]|1[012])(?:[-/.](?:0?[1-9]|[12][0-9]|3[01]))?[-/.](?:19|20)?\d\d\b 应该处理“日期(例如 1/31/2020、3/20)”的情况
-
(?i)\bEnd(?: DATE|(?:ing)?)\b 应该处理“像“Ending”、“ENDING”、“END”、“end”、“End”、“END DATE”这样的“字符串”,而不是像“endemic”这样的字符串中的“end”最后一个”案例
-
([\s»]){2,} 应该处理“双空格,例如”“”的情况。
结合所有:
gsub("\\b(?:End(?:\\s+DATE|(?:ing)?)|(?:0?[1-9]|1[012])(?:[-/.](?:0?[1-9]|[12][0-9]|3[01]))?[-/.](?:19|20)?\\d\\d)\\b|([\\s»]){2,}", "\\1", x, perl=TRUE, ignore.case=TRUE)
见proof。
说明
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
End 'End'
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
DATE ' DATE'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
ing 'ing'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[012] any character of: '0', '1', '2'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
[-/.] any character of: '-', '/', '.'
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[12] any character of: '1', '2'
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
3 '3'
--------------------------------------------------------------------------------
[01] any character of: '0', '1'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
[-/.] any character of: '-', '/', '.'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
19 '19'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
20 '20'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \1 (at least 2 times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[\s»] any character of: whitespace (\n, \r,
\t, \f, and " "), '»'
--------------------------------------------------------------------------------
){2,} end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)