Php 从带有括号、方括号和连字符的字符串中获取子字符串
Regex不是我最擅长的,我在这种情况下有点麻烦 我有以下字符串:Php 从带有括号、方括号和连字符的字符串中获取子字符串,php,regex,Php,Regex,Regex不是我最擅长的,我在这种情况下有点麻烦 我有以下字符串: locale (district - town) [parish] 我需要提取以下信息: 1-区域设置 2-地区 三镇 我有以下解决方案: 1-区域设置 preg_match("/([^(]*)\s/", $input_line, $output_array); 2-地区 preg_match("/.*\(([^-]*)\s/", $input_line, $output_array); 三镇 preg_match("/.
locale (district - town) [parish]
我需要提取以下信息:
1-区域设置
2-地区
三镇
我有以下解决方案:
1-区域设置
preg_match("/([^(]*)\s/", $input_line, $output_array);
2-地区
preg_match("/.*\(([^-]*)\s/", $input_line, $output_array);
三镇
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)
地区+地区+镇A镇B
我发现很难处理所有这些情况。你能帮我吗
提前感谢如果地区、地区和城镇中没有空间:
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
----------------------------------------------------------------------
不确定您的规则和边缘情况到底是什么,但这适用于提供的示例
preg_match('#^(.+?) \((.+?) - (.+?)\) \[(.+)\]$#',$str,$matches);
给出这些结果(当对$str
中的每个示例字符串运行时):
@哈姆扎:为什么要评论,为什么不回答?@anubhava我正忙于其他事情,这是一个快速的小提琴。如果我把它作为一个答案发布,我至少应该提供一些解释。@hamza:好的,我理解,但是你的努力值得一个答案和一些投票。@anubhava别担心,有人会在几分钟内复制/粘贴到一个答案:)
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
)