简体   繁体   English

正则表达式返回php中的数字

[英]Regular expression to return a number in php

Hi i have a string and would like to search for the string that matches for example 嗨,我有一个字符串,想搜索例如匹配的字符串

[table id=345 /] [表格ID = 345 /]

and would like it to return the digit 345. May I know what is the rule? 并希望它返回数字345。请问这是什么规则?

My rule is this: 我的规则是这样的:

preg_match("/\[table id=([^]*?) \/\]/s", $char, $match);

but not working. 但不起作用。

How to get it return if there is more than one that match the pattern? 如果有多个匹配模式,如何获得返回值? at the moment it only return the first occurrence. 目前,它仅返回第一次出现。

尝试这个:

preg_match("@\[table ([0-9]+) \/\]@s",$char,$match);

Here is your regex: 这是您的正则表达式:

\[table id=\K\d+

This sample code prints all the matches (see output at the bottom of the online dem o): 此示例代码显示所有匹配项(请参见在线演示底部的输出):

$string = "[table id=345 /] [table id=123 /]
[table id=999 /] [table id=000 /]";
$regex = "~\[table id=\K\d+~";
$count = preg_match_all($regex,$string,$m);
print_r($m[0]);

Explain Regex 解释正则表达式

\[                       # '['
table id=                # 'table id='
\K                       # Keep what has been matched so far out of the returned match
\d+                      # digits (0-9) (1 or more times (matching
                         # the most amount possible))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM