简体   繁体   English

如何忽略正则表达式中的字符

[英]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. 仅需要获取P1,但这将返回/ 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: 从组中排除它, P1将是捕获组中的内容:

     $pattern = "#/(P[0-9])#"; 
  • Use a lookbehind so that the / isn't even a part of the match, the entire match will be P1 : 使用后退标记,以使/甚至不是比赛的一部分,整个比赛将为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])? 在您的小组之后,因为我认为您实际上并不想要它,这使得前一个元素是可选的,因此正则表达式(/P[0-9])? would match literal any string (it would match an empty string if /P[0-9] could not be matched). 将与文字上的任何字符串匹配(如果/P[0-9]无法匹配,则它将匹配空字符串)。

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

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

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

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