简体   繁体   中英

how to ignore a character in the regular expression

I do not know how to ignore an item from the ER.

just need to get P1, but this returns /P1.

is possible to just ignore the bar?

$pattern = "#(/P[0-9])?#";

There are two options here:

  • Exclude it from the group, P1 will be the contents in the capture group:

     $pattern = "#/(P[0-9])#"; 
  • Use a lookbehind so that the / isn't even a part of the match, the entire match will be P1 :

     $pattern = "#(?<=/)P[0-9]#"; 

Note that I also removed the ? after your group because I don't think you actually want it, this makes the previous element optional so the regex (/P[0-9])? would match literal any string (it would match an empty string if /P[0-9] could not be matched).

使用preg_ *函数,您可以使用\\K技巧来重置比赛开始,例如:

$pattern = '~/\KP[0-9]~';

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