描述
该表达式将:
- 匹配您的所有示例案例,同时避免不希望的案例
- 符合 java 正则表达式引擎
请注意,这确实使用了x
忽略表达式中的空格和注释的选项
(?:^|\s)(?:Year|YEAR)(?:\s|\Z|$) # match no prefix or suffix
|
(?:^|\s)[A-Z][a-z]+[^a-zA-Z\d]?(?:Year|YEAR)(?:\s|\Z|$) # match title case prefix
|
(?:^|\s)[A-Z]+[^a-zA-Z\d\s](?:Year|YEAR)(?:\s|\Z|$) # match all uppercase prefix
|
(?:^|\s)[a-z]+[^a-zA-Z\d](?:year)(?:\s|\Z|$) # match all lower case prefix and keyword
|
(?:^|\s)(?:Year|YEAR)[^a-zA-Z\d]?[A-Z][a-z]+(?:\s|\Z|$) # match title case suffix
|
(?:^|\s)(?:Year|YEAR)[^a-zA-Z\d\s][A-Z]+(?:\s|\Z|$) # match all uppercase suffix
或作为单个字符串:
(?:^|\s)(?:Year|YEAR)(?:\s|\Z|$)|(?:^|\s)[A-Z][a-z]+[^a-zA-Z\d]?(?:Year|YEAR)(?:\s|\Z|$)|(?:^|\s)[A-Z]+[^a-zA-Z\d\s](?:Year|YEAR)(?:\s|\Z|$)|(?:^|\s)[a-z]+[^a-zA-Z\d](?:year)(?:\s|\Z|$)|(?:^|\s)(?:Year|YEAR)[^a-zA-Z\d]?[A-Z][a-z]+(?:\s|\Z|$)|(?:^|\s)(?:Year|YEAR)[^a-zA-Z\d\s][A-Z]+(?:\s|\Z|$)
例子
现场示例:http ://www.rubular.com/r/QTUNDPKuOL
示例文本
Order YEAR
OrderYear
Order_YEAR
ORDER_YEAR
order year
YEAR_Order
YearOrder
But not these:
orderyear
ORDERYEAR
yearning
Order_Year_Test
匹配上
[0] => Order YEAR
[1] =>
OrderYear
[2] =>
Order_YEAR
[3] =>
ORDER_YEAR
[4] =>
order year
[5] =>
YEAR_Order
[6] =>
YearOrder