2

正则表达式不是我最擅长的,我在这种情况下遇到了一些麻烦。

我有以下字符串:

locale (district - town) [parish]

我需要提取以下信息: 1 - 地区 2 - 地区 3 - 城镇

我有这些解决方案:

1 - 语言环境

preg_match("/([^(]*)\s/", $input_line, $output_array);

2 - 区

preg_match("/.*\(([^-]*)\s/", $input_line, $output_array);

3 - 城镇

preg_match("/.*\-\s([^)]*)/", $input_line, $output_array);

这些似乎工作正常。但是,字符串可以像以下任何一种方式呈现:

localeA(localeB) (district - town) [parish]
locale (district - townA(townB)) [parish]
locale (district - townA-townB) [parish]

语言环境也可以包含它自己的括号。Town 可以包含括号和/或它自己的连字符。

这使得难以提取正确的信息。在上面的 3 个场景中,我必须提取:

localeA(localeB) + 地区 + 城镇

语言环境 + 地区 + 城镇 A(城镇 B)

语言环境 + 地区 + townA-townB

我发现很难处理所有这些情况。你能帮我吗?

提前致谢

4

2 回答 2

0

如果语言环境、地区和城镇中没有空格:

preg_match("/^\s*(\S+)\s*\((\S+)\s*-\s*(\S+)\)/", $input_line, $output_array);

解释:

The regular expression:

(?-imsx:^\s*(\S+)\s*\((\S+)\s*-\s*(\S+)\))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  -                        '-'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    \S+                      non-whitespace (all but \n, \r, \t, \f,
                             and " ") (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-08-21T08:28:13.767 回答
0

不确定您的规则和边缘情况到底是什么,但这适用于提供的示例

preg_match('#^(.+?) \((.+?) - (.+?)\) \[(.+)\]$#',$str,$matches);

给出这些结果(针对 中的每个示例字符串运行时$str):

Array
(
    [0] => locale (district - town) [parish]
    [1] => locale
    [2] => district
    [3] => town
    [4] => parish
)

Array
(
    [0] => localeA(localeB) (district - town) [parish]
    [1] => localeA(localeB)
    [2] => district
    [3] => town
    [4] => parish
)

Array
(
    [0] => locale (district - townA(townB)) [parish]
    [1] => locale
    [2] => district
    [3] => townA(townB)
    [4] => parish
)

Array
(
    [0] => locale (district - townA-townB) [parish]
    [1] => locale
    [2] => district
    [3] => townA-townB
    [4] => parish
)
于 2013-08-21T09:15:59.393 回答