RegEx匹配整个世界或以单词和/或结尾开头 - 用另一个单词

时间:2017-01-19 06:56:45

标签: php regex pcre

我希望有人可以协助使用符合以下条件的RegEx: -

  • 局部性
  • 局部性
  • 处所名称
  • LOCALITY_NAME
  • 局部性名称
  • 郊区
  • 市郊
  • SuburbName
  • suburb_name
  • 郊区名

NOT 匹配: -

  • locality_pid
  • locality_index
  • LocalityRef
  • street_locality_pid
  • street_suburb_pid

我目前使用的RegEx: -

JSONArray jsonArray = new JSONArray(response);

// Here you are getting the program JSONArray
JSONArray jsonProgramArray = jsonArray.getJSONArray("program_name");

for (int i = 0; i < jsonProgramArray.length() - 1; i++) {
    // Get the each Json Object from the Array
    JSONObject jsonProgramObject = jsonProgramArray.getJSONObject(i);

    String program_name = jsonProgramObject.getString("program_name") 
}

匹配除“locality”和“suburb”

这些确切单词之外的所有内容

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

Explanation

示例代码:

<?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