0

Say I wish to find every iteration of orange that is not preceded by apple and using RegEx. I also want this to work for the respective plurals.

Here's a list of what should and shouldn't be valid.

orange             - valid
oranges            - valid
an orange          - valid
apple and orange   - invalid
apple and oranges  - invalid
apples and orange  - invalid
apples and oranges - invalid

The code I've written to make this possible so far is the following.

(?<!apples? and )oranges?

When I test it, I receive an error, tracing back to the optional s character, after apple. Regex101 describes the error as the following.

Lookbehinds need to be zero-width, thus quantifiers are not allowed.

Why is this? How can I solve this problem?

4

1 回答 1

3

后视必须有一个固定的长度。通过指定一个可选字符,您表示长度可以变化 1。这是不被接受的。但是,您不受执行的后视次数的限制,因此您可以将其分成两部分,如下所示:

(?<!apple and )(?<!apples and )oranges?
于 2016-07-02T15:31:54.467 回答