razkal
razkal

Reputation: 35

Regex multiple values

Looking for a simple regex to be used in Google Analytics as a filter in customized channels to capture multiple values as part of a URL.

For example:

www.abc.com/fruit/apple/
www.abc.com/fruit/banana/
www.abc.com/fruit/pear/
www.abc.com/fruit/strawberry/

If I just want to capture apple, banana and pear, I thought a simple apple|banana|pear or (apple|banana|pear) would do the trick, but it doesn't.

What is missing?

Upvotes: 1

Views: 4400

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

Since you are using matches regex option, the pattern seems to be anchored. So, you need to enclose the whole alternation group with .*:

.*(apple|banana|pear).*

To match these words as whole words, use

(.*\W|^)(apple|banana|pear)(\W.*|$)

where (.*\W|^) matches any 0+ chars as many as possible and then a non-word char (.*\W), OR (|) the start of string (^). $ matches the end of string.

Upvotes: 1

Related Questions