Freshblood
Freshblood

Reputation: 6431

Simplifying multiple Or Regex query

I need to detect that current text contains following keywords "guest post", "guest post", "guest contributor","guest-etc".

i can write query as below.

"guest.?post|guest.?write|guest.?contributor|guest.?etc"

I am looking for a way to make this regex query shorter.

Upvotes: 1

Views: 48

Answers (2)

Josh Crozier
Josh Crozier

Reputation: 240978

If "guest" is constant, then you can move it out of the alternations:

Example Here

guest.?(?:post|write|contributor|etc)

Upvotes: 2

Barmar
Barmar

Reputation: 781255

Put the common prefix outside a group, and the alternatives inside the group.

guest.?(?:post|write|contributor|etc)

Upvotes: 2

Related Questions