1

I am hoping someone can assist with a RegEx that will match the following:-

  • locality
  • Locality
  • LocalityName
  • locality_name
  • locality-name
  • suburb
  • Suburb
  • SuburbName
  • suburb_name
  • suburb-name

But will NOT match for:-

  • locality_pid
  • locality_index
  • LocalityRef
  • street_locality_pid
  • street_suburb_pid

The RegEx I am currently using:-

/^(locality|suburb)(?=.*name).*$/img

Matches all except the exact words 'locality' and 'suburb'

Trent Renshaw
  • 422
  • 6
  • 13

1 Answers1

1

You can try this:

'/^(?:locality|suburb)(?:(?:-|_)?name)?$/im'

Explanation

Sample Code:

<?php

$re = '/^(?:locality|suburb)(?:(?:-|_)?name)?$/im';
$str = 'locality
Locality
LocalityName
locality_name
locality-name
suburb
Suburb
SuburbName
suburb_name
suburb-name
locality_pid
locality_index
LocalityRef
street_locality_pid
street_suburb_pid
';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

?>

Run here

Rizwan M.Tuman
  • 9,424
  • 2
  • 24
  • 40
  • Thanks Maverick! I can be greedy with the character between 'locality' or 'suburb' and 'name' so have modified as follows:- /^(?:locality|suburb)(?:.*name)?$/img – Trent Renshaw Jan 19 '17 at 07:27