简体   繁体   English

在php中创建正则表达式

[英]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 Preg match伪代码: logs/{24 digitals}/{same24digits}.log

Both \\d{24} are the same number 两个\\d{24}是相同的数字

Question How to get the : If matched return \\d{24} in echo 问题如何获取:如果匹配,则在回显中返回\\ d {24}

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}) \\1 ia反向引用第一个捕获的组,即(\\d{24})

Code Demo 代码演示

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

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

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

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