字母后的正则表达式编号

时间:2018-01-25 09:45:14

标签: php regex

我有这个正则表达式:

/\d+(?!\d| |[a-zA-Z])/gm

和这个字符串:

,98dépo 58 cou54lis 
GU966,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000
1600.00,1841.00,1882.80,1898.50,1914.30,1930.00,1945.80,1993.40,2009.20,2028.50,2044.30,2061.50,2095.30,2111.1

我不想匹配',98dépo58cou54lis GU966'中的任何数字,但所有其他数字都是肯定的。

我该怎么做?
感谢

3 个答案:

答案 0 :(得分:3)

使用理智的解决方案:

$numbers = array_filter(explode(",", str_replace(["\r\n","\n","\r"],",", $string)), 'is_numeric');

演示:http://sandbox.onlinephpfunctions.com/code/958360336537ba5c4c99cba7e18419738045f407

答案 1 :(得分:2)

模式:~[\v,]\K\b[.\d]+\b~

Pattern Demo

所有有效号码前面都有逗号或垂直空格。

$string=',98dépo 58 cou54lis 
GU966,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000
1600.00,1841.00,1882.80,1898.50,1914.30,1930.00,1945.80,1993.40,2009.20,2028.50,2044.30,2061.50,2095.30,2111.1';

var_export(preg_match_all('~[\v,]\K\b[.\d]+\b~',$string,$out)?$out[0]:[]);

输出:

array (
  0 => '1800',
  1 => '1900',
  2 => '2000',
  3 => '2100',
  4 => '2200',
  5 => '2300',
  6 => '2400',
  7 => '2500',
  8 => '2600',
  9 => '2700',
  10 => '2800',
  11 => '2900',
  12 => '3000',
  13 => '1600.00',
  14 => '1841.00',
  15 => '1882.80',
  16 => '1898.50',
  17 => '1914.30',
  18 => '1930.00',
  19 => '1945.80',
  20 => '1993.40',
  21 => '2009.20',
  22 => '2028.50',
  23 => '2044.30',
  24 => '2061.50',
  25 => '2095.30',
  26 => '2111.1',
)

apokryfos方法的这种调整返回与我的正则表达式方法相同的预期结果。

var_export(array_filter(explode(',',str_replace(PHP_EOL,',',$string)),'is_numeric'));

它只是通过将换行符转换为逗号来准备爆炸字符串。

答案 2 :(得分:0)

您可以使用此正则表达式:

/\n?([,\n](\d{1,}\.?\d{0,}))/

用法:

$pattern = '/\n?([,\n](\d{1,}\.?\d{0,}))/';
preg_match_all($pattern, $str, $match);

$values = $match[2];