I am searching for a very specific regex expression that will help me find strings that meet the following rules:

  1. The string must contain the keyword 'Year' or 'YEAR'
  2. The keyword needs to be separated from other text by non-letters OR the keyword is in all caps or first letter capitalized in a string that is otherwise mixed cased.
  3. The keyword must be either at the beginning or the end of the string.

For example, I would like the regex to match the following strings:

Order YEAR
OrderYear
Order_YEAR
ORDER_YEAR
order year 
YEAR_Order
YearOrder

But not these:

orderyear
ORDERYEAR
yearning
Order_Year_Test

The only thing I could come up with so far is:

^YEAR|YEAR$|^Year|Year$

Which works for most things but returns the opposite output for the "order year" and "ORDERYEAR" examples. I need some sort of regex expresssion that expresses casing rules.

Specifically I am using ICU's regex library (but just general regex advice is fine).

Thanks for any help,

有帮助吗?

解决方案

Description

This expression will:

  • match all your sample cases, while avoiding the undesired cases
  • comply with a java regex engine

Note this does use the x option which ignores whitespace and comments in the expression

(?:^|\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

Or as a single string: (?:^|\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|$)

Example

Live example: http://www.rubular.com/r/QTUNDPKuOL

Sample Text

Order YEAR
OrderYear
Order_YEAR
ORDER_YEAR
order year 
YEAR_Order
YearOrder
But not these:

orderyear
ORDERYEAR
yearning
Order_Year_Test

Matched on

[0] => Order YEAR
[1] => 
OrderYear
[2] => 
Order_YEAR
[3] => 
ORDER_YEAR
[4] => 
order year 
[5] => 
YEAR_Order
[6] => 
YearOrder
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top