简体   繁体   English

正则表达式排除一切,但个别地

[英]Regex exclude everything but, individually

Current Regex: 当前正则表达式:

([^ipd])

I get the correct output, although it doesn't deselect them individually. 我得到正确的输出,尽管它没有单独取消选择它们。 I'm wanting to deselect only i , p and d individually. 我只想分别取消选择ipd

Want: 想:

i
p
d

Don't want: 不想:

ipd
ip
pd
id

etc. 等等

I've tried: 我试过了:

([^ipd] {1})
([^i|p|d$])
^[ipd]$

You need to add anchors to limit the matches to just i,p,d . 您需要添加锚点以将匹配项限制为i,p,d

$re = "/^[ipd]$/m"; 
$str = "i\np\nd\n\nDon't Want\nipd\nip\npd\nid"; 

preg_match_all($re, $str, $matches);

This work in string and with separate characters: 此项以字符串形式工作,并带有单独的字符:

[^ipd]|(?<=[ipd])[ipd]|[ipd](?=[ipd])

where: 哪里:

  • [^ipd] - not "i","p" or "d"; [^ipd] -不是“ i”,“ p”或“ d”;
  • (?<=[ipd])[ipd] - match "i","p" or "d" if it is proceeded by "i","p" or "d"; (?<=[ipd])[ipd] -如果以“ i”,“ p”或“ d” (?<=[ipd])[ipd] ,则匹配“ i”,“ p”或“ d”;
  • [ipd](?=[ipd]) - "i","p" or "d" if it is fallowed by "i","p" or "d"; [ipd](?=[ipd]) -如果“ i”,“ p”或“ d”被赋予“ i”,“ p”或“ d”;

DEMO DEMO

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

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