preg_match_all使用

jer*_*mib 2 php regex

我有一个始终遵循以下格式的字符串:

This Fee Name :  *  Fee Id  * Fee Amount  $* is required for this activity
Run Code Online (Sandbox Code Playgroud)

例:

This Fee Name :  STATE TITLE FEE  Fee Id  2 Fee Amount  $5.50 is required for this activity
Run Code Online (Sandbox Code Playgroud)

我想用PHP做的是传递字符串并获得结果

  • STATE TITLE FEE
  • 2
  • 5.50

我很确定preg_match_all我想要的,但无法弄清楚如何正确使用正则表达式.

小智 5

实际上,您可以使用preg_match括号来使用和捕获所需的部分(请注意,?:括号内部表示括号仅用于分组(即可能有小数点和美元金额后的一个或多个数字) ).(警告:未经测试,但这应该有效.)

$str="This Fee Name :  STATE TITLE FEE  Fee Id  2 Fee Amount  $5.50 is required for this activity";

if(preg_match('/^This Fee Name :\s+(.*)\s+Fee Id\s+(\d)\s+Fee Amount\s+(\$\d+(?:\.\d+)?)\s+is required for this activity$/',$str,$matches))
{
  $fee_name=$matches[1];
  $fee_id=$matches[2];
  $fee_amount=$matches[3];
}
else
{
  //No matches!  Do something...or not.  Whatever.
}
Run Code Online (Sandbox Code Playgroud)