0

I have the following basic RegEx expression in Google Analytics that consists of a list of city names.

(toronto|vancouver|calgary|halifax|winnipeg|ottawa|edmonton|regina|mississauga)

I would like to add another expression to the query such that a result is not returned if the matching text also has the word "job" in it.

zgall1
  • 2,865
  • 5
  • 23
  • 39

2 Answers2

0

[Edited answer] This works:

^((?!job).)*(toronto|vancouver|calgary|halifax|winnipeg|ottawa|edmonton|regina|mississauga)(?!.*job)

It matches the city names only if "job" is not present before or after the city name.

See https://regex101.com/r/tXfBGf/3

Pierre François
  • 5,850
  • 1
  • 17
  • 38
-1

Provided your flavor of regex includes capture groups, this is easily solved using The Best Regex Trick, prior exclusion. Essentially:

"string".match(/(?:a|b|c)job|(a|b|c)/).match_group(1), where:
?:: Do not save the prior exclusion in a capture group;
(a|b|c)job are the strings to avoid capturing;
|(a|b|c) are the strings to capture.

This works because if the text matches the undesirable text, ajob, then the capture group is never populated. It will only be populated if the desired text a is matched without ajob

Devon Parsons
  • 1,234
  • 14
  • 23
  • I am not able to follow how this concept can be applied to my specific case. – zgall1 Jan 17 '20 at 16:11
  • If you can write a regex that matches the text you don't want (e.g text matching /job/) then you can use this technique to convert that to matching on text you DO want while excluding those forbidden contexts. – Devon Parsons Jan 17 '20 at 16:34
  • Perhaps if you can clarify what you mean by "has the word 'job' in it" I can write a more specific regex to your case; for example, are you encountering text like `winnijobpeg`? I doubt it, so I used the simpler example of city followed by job a la `winnipegjob` – Devon Parsons Jan 17 '20 at 16:36
  • The word job is always separated from other characters by either a slash (/job/winnipeg) or a dash (job-winnipeg). – zgall1 Jan 17 '20 at 20:08
  • then use`/job[-\/](?:winnipeg|toronto|...)|(winnipeg|toronto|...)/`, match group 1. Sample here: https://regex101.com/r/jBfTeE/1 Pay close attention to the match group panel on the right side. – Devon Parsons Jan 20 '20 at 18:09