简体   繁体   English

PHP中特定字符串的正则表达式

[英]Regular Expression of a particular String in PHP

I have this few strings: 我有几个字符串:

'user/1/myid_print'
'user/2/myid_print'
'user/3/myid_print'
'user/4/myid_print'
'user/5/myid_print'

The second part is the dynamic one which must contain only integers. 第二部分是动态部分,必须仅包含整数。 What is it's regular expression? 它是什么正则表达式?

Try this: 尝试这个:

/user\/\d+\/myid_print/

the \\d+ matches a number which contains at least one digit. \\d+与包含至少一位数字的数字匹配。 if it is a non-zero number, replace the \\d+ with [1-9]\\d* 如果它是一个非零数字,请将\\d+替换为[1-9]\\d*

It depends a bit on what language you're using, but one valid answer for Python is: 这取决于您使用的是哪种语言,但是Python的一个有效答案是:

user/\d/myid_print

The / character is often used in describing regular expressions and sometimes needs \\ before it to make it match part of the string, a valid answer might be: /字符通常用于描述正则表达式,有时需要\\使其与字符串的一部分匹配之前,有效的答案可能是:

user\/\d\/myid_print

These match the text "user/" and "/myid_print" literally, and \\d matches the pattern "any digit 0-9". 它们从字面上匹配文本“ user /”和“ / myid_print”, \\d匹配模式“任何数字0-9”。 If you need to match the numbers 1,2,3,4,5 only, then use [1-5] instead of \\d 如果只需要匹配数字1,2,3,4,5,则使用[1-5]代替\\d

Test it here: https://regex101.com/r/mS4xN4/1 在这里测试: https : //regex101.com/r/mS4xN4/1

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

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