我正在搜索一个非常具体的正则表达式,它将帮助我找到符合以下规则的字符串:
例如,我希望正则表达式匹配以下字符串:
Order YEAR
OrderYear
Order_YEAR
ORDER_YEAR
order year
YEAR_Order
YearOrder
但不是这些:
orderyear
ORDERYEAR
yearning
Order_Year_Test
到目前为止我唯一能想到的是:
^YEAR|YEAR$|^Year|Year$
哪些适用于大多数事情,但返回“订单年”和“ORDERYEAR”示例的相反输出。我需要某种表达套管规则的正则表达式。
具体来说我正在使用ICU的正则表达式库(但只是一般的正则表达式建议很好)。
感谢您的帮助,
答案 0 :(得分:1)
此表达式将:
注意这确实使用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