我希望从此字符串中获取America/Chicago
Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))
并让其适用于其他时区,例如America/Los_Angeles
,America/New_York
等。我对prey_match_all
并不是很好,如果有人可以指导我如何正确地学习它,那么这是我第三次需要使用它。
答案 0 :(得分:2)
这是你的正则表达式的解决方案
<强>代码强>
$in = "Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))";
preg_match_all('/\(([A-Za-z0-9_\W]+?)\\s/', $in, $out);
echo "<pre>";
print_r($out[1][0]);
?>
和OUTPUT
America/Chicago
希望这对您有所帮助。
答案 1 :(得分:0)
我会使用正则表达式:\((\S+)
preg_match_all('/\((\S+)/', $in, $out);
<强>解释强>
The regular expression:
(?-imsx:\((\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):
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
( 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
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------