0

I'm trying to sort of do a two-in-one regex match for GA keywords

Want to say the following, that either the optional prefix or one (or both) of the optional suffixes must be present to match – along with the 'required middle' string, 'people' in this case

So only these combinations would match:

people
happy people
happy people smile
happy people smile often
people smile
people smile often

This would not match:

happy people smile often in Connecticut

Or anything else for that matter

I've never used lookaheads; I'm guessing this might be the solution(?)

Open to other solutions if there are any

Again, I'm using Google Analytics if someone needs to know what regex flavour

While the above list is useful, I also need to know how to do it so that "people" alone would not match for some queries, but only lines 2-6 in the example.

So I guess I really need two expressions

4

1 回答 1

1

这应该这样做。由于people在所有变体中都很常见,因此我们将其用作确定匹配(否则您最终会匹配任何内容而不匹配任何内容)。所以happy不是必需的,我们添加?量词来表示它是否存在。另请注意,我在分组括号内包含了一个空格。Nextpeople始终是必需的,后跟smileor smile often(带有相应的空格)。现在,如果您想要这些不同短语中的每一个的指标,则需要将它们设置为不同的过滤器。下面的代码为您列出的所有单词返回 true。我们将^用于行首和行$尾,因此我们可以确保在较长的短语中不会为这些单词返回 true。

^(happy )?people( smile| smile often)?$

根据您的额外要求,为简单起见,我们可以将其or完全作为一个声明。我以这种方式订购它是为了提高速度,因为 REGEX 从左到右检查。如果遇到不存在的空格,它将转到or.

^(people smile often|people smile|happy people smile often|happy people smile|happy people)$
于 2013-08-14T19:26:40.203 回答