从":"之间的字符串获取值和","

时间:2015-04-01 13:49:17

标签: regex string preg-match

以下是我的字符串

的示例
"{"id":128,"order":128,"active":"1","name":"\"

现在我需要得到" 128" - id参数。所以它的第一个值是":"和","。

我尝试过使用preg_match和不同的正则表达式,但我在正则表达式中表现不佳。也许有人会知道如何制作它?

$id = preg_match('/:(,*?)\,/s', $content, $matches);

2 个答案:

答案 0 :(得分:1)

以下是使用正则表达式获取第一个:之后的数字的示例代码:

$re = "/(?<=\\:)[0-9]+/"; 
$str = "\"{\"id\":128,\"order\":128,\"active\":\"1\",\"name\":\"\""; 
preg_match($re, $str, $matches);
print $matches[0];

以下是TutorialsPoint上的示例程序。

关于此正则表达式(?<=\\:)[0-9]+的一个小细节:幸运的是,它使用固定宽度look-behind that PHP supports

答案 1 :(得分:0)

<?php


 $txt='"{"id":128,"order":128,"active":"1","name":"\\"';

 $re1='.*?';    # Non-greedy match on filler
 $re2='(\\d+)'; # Integer Number 1
 $re3='.*?';    # Non-greedy match on filler
 $re4='(\\d+)'; # Integer Number 2
 $re5='.*?';    # Non-greedy match on filler
 $re6='(\\d+)'; # Integer Number 3

   if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6."/is",$txt, $matches))
  {
  $int1=$matches[1][0];
  $int2=$matches[2][0];
  $int3=$matches[3][0];
  print "($int1) ($int2) ($int3) \n";
  }

  ?>
相关问题