I want a regex in php that matches statements like : {{User:JYBot/count|700,000}}
commnet : only the number 700,000 is variable and other are fixed.
I wrote this : '/\{\{User:JYBot/count\|\d{1,10}\}\}/'
but it does not work properly, because of existence of /
in it.this regex too does not work properly : '/\{\{User:JYBot\/count\|\d{1,10}\}\}/'
Please help me about it. Regards
Some little how-to. Take your string:
{{User:JYBot/count|700,000}}
Enclose the parts that should be matched verbatim into \Q...\E
:
\Q{{User:JYBot/count|\E ... \Q}}\E
Now replace the dynamic part 700,000
with its pattern, e.g. [\d,]{1,10}
:
\Q{{User:JYBot/count|\E[\d,]{1,10}\Q}}\E
Add the delimiters (there are more than /
, take one not part of the string so far or something with parenthesis like ()
/{}
or[]
) and you are done:
~\Q{{User:JYBot/count|\E[\d,]{1,10}\Q}}\E~
You either need to escape your slashes or use different delimiters:
escaping slashes:
'/\{\{User:JYBot\/count\|\d{1,10}\}\}/'
different delimiters:
'~\{\{User:JYBot/count\|\d{1,10}\}\}~'
You need to escape the /
from the regular expression parser, which means that the parser needs to see a \
before it. However, the PHP string expresion "\/"
is going to evaluate as "/"
-- you escape the forward slash from PHP's string literal parser, and so the \
character does not wind up being part of the final string.
In order get an actual \
in the string, use two of them:
'/\{\{User:JYBot\\/count\|\d{1,10}\}\}/'
The regular expression parser will then see the \/
sequence you intend.
Try the following:
/\{{2}User:JYBot\/count\|([0-9\,]+)\}{2}/is
You will, however, get the comma in the number, so you'll need to str_replace it away.
This should work, assuming the number is split with commas:
/\{\{User:JYBot\/count\|\d{1,3}(?:,\d{3})*\}\}/