user2249813
user2249813

Reputation: 25

PCRE Regex help needed

I am having trouble writing a perl compatible regex to match a few different things when there is a gap between each condition. It makes more sense when I explain what I want it to find

Conditions

  1. /world/
  2. a single letter
  3. a dash OR underscore
  4. a single letter
  5. a single period
  6. three or four letters

The problem I have is I don't know how to write the expression such that there can be a gap between condition #1 and #2. Conditions #2 - #4 can repeat, but not always.

I've been using multiple online regex testers but I cannot get a match and I am not sure what I am doing wrong. I think regex is looking for /world/x_x or /world/y-y instead of "looking ahead" to match on the "letter dash letter" or "letter underscore letter" pattern.

Current regex

/world/([a-z](-|_)[a-z]){1,}\.[a-z]{3,4}$

Desired Matches (not currently matching)

hxxp://armassimchilzeispreu.blackjackipad.com/world/activate_available.jar

hxxp://chubfaceddamsel0.affittobarcheavela.it/world/eternal_threat-clearing.html

hxxp://offdestroyengarabitar.freebookofraslot.com/world/bonus-middle-marathon.pdf

Upvotes: 1

Views: 116

Answers (1)

Borodin
Borodin

Reputation: 126722

I think you want this

use strict;
use warnings;

while (<DATA>) {
  chomp;
  print "OK $_\n" if m</world/[a-z]+(?:[_-][a-z]+)+\.[a-z]{3,4}$>;
}

__DATA__
hxxp://armassimchilzeispreu.blackjackipad.com/world/activate_available.jar
hxxp://chubfaceddamsel0.affittobarcheavela.it/world/eternal_threat-clearing.html
hxxp://offdestroyengarabitar.freebookofraslot.com/world/bonus-middle-marathon.pdf

or perhaps just

m</world/[a-z-_]+\.[a-z]{3,4}$>

Upvotes: 3

Related Questions