我需要使用以下格式在文档中搜索一些文本:
(#.#.#) ex; (1.4.6)
这可能很简单,但它超出了我的正则表达式技能。
答案 0 :(得分:7)
您可以使用以下正则表达式:
\(\d{1,2}\.\d{1,2}\.\d{1,2}\)
示例PHP:
<?php
$str = "(1.12.12) some text (1.1.1) some other text (1.1232.1) text";
preg_match_all('/\(\d{1,2}\.\d{1,2}\.\d{1,2}\)/',$str,$matches);
print_r($matches);
?>
输出:
Array
(
[0] => Array
(
[0] => (1.12.12)
[1] => (1.1.1)
)
)
如果您想要可以包含任意数量的数字(&gt; 0),请使用以下正则表达式:
\(\d+\.\d+\.\d+\)
答案 1 :(得分:1)
你试过这个吗?
$int = preg_match("/\(\d{1,2}\.\d{1,2}\.\d{1,2}\)/", "(11.2.33)", $matches);
您可以在此处http://micmap.org/php-by-example/en/function/preg_match
进行测试