简体   繁体   中英

Creating regular expression in php

My attempt

preg_match("/^logs.\d{24}.\d{24}.{4}$/", $input_line, $output_array);
If matched return \d{24} in echo

String

logs/532523532543353444444444/532523532543353444444444.log

Preg match peseudo-code : logs/{24 digitals}/{same24digits}.log

Both \\d{24} are the same number

Question How to get the : If matched return \\d{24} in echo

You can use:

if (preg_match('~^logs/(\d{24})/\1\.log$~', $input_line, $m))
   echo $m[1];

\\1 ia back-reference to first captured group ie (\\d{24})

Code Demo

logs\/(\d{24})\/\1.log

参见演示http://regex101.com/r/hU0uS6/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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