找到所有包含“p”但不是“ph”的词

时间:2021-04-12 06:24:14

Say,

说,

str = 'python php ruby javascript jsonp perhapsphpisoutdated'

How do you find all words that contains p but not ph using regex?

如何找到包含p但不使用正则表达式的所有单词?

I know how to solve it with split&filter:

我知道如何用split&filter来解决它:

str.split(' ')
  .filter( w=>w.includes('p') && !w.includes('ph') )

Is there a possible regex way?

有没有可能的正则表达式?

4 个解决方案

#1


9  

You may use the following solution:

您可以使用以下解决方案:

var str = 'python php ruby javascript jsonp perhapsphpisoutdated';
var res = str.match(/\b(?!\w*ph)\w*p\w*/g);
console.log(res);

Description:

描述:

  • \b - leading word boundary
  • \b -引导词边界。
  • (?!\w*ph) - a negative lookahead requiring that there cannot be ph after 0+ word chars that appear right after the leading word boundary
  • (? \w*ph) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  • \w* - 0+ word chars
  • \w* - 0+单词chars。
  • p - a letter p
  • p -字母p。
  • \w* - 0+ word chars.
  • \w* - 0+单词chars。

Note that \w matching letters, digits and underscores can be replaced with [a-zA-Z] to only match ASCII letters.

注意,可以用[a-zA-Z]替换字母、数字和下划线来匹配ASCII字母。

Visualization:

可视化:

找到所有包含“p”但不是“ph”的词

#2


1  

This is actually something you could do a number of different ways. I suggest you use https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ and look at the quantifiers and matching. You might also read up on look ahead http://www.regular-expressions.info/lookaround.html

这实际上是你可以用不同的方法做的事情。我建议你使用https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/,查看量词和匹配。你也可以阅读http://www.regular-expressions.info/lookaround.html。

Here are a couple of ideas.

这里有一些想法。

p{1}h{0} this says exactly 1 p followed by exactly 0 h

p{1}h{0}正好是1 p,后面正好是0 h。

p[^h] this says p followed by anything which is not h (this won't match "p" by itself)

h p[^]随后p说什么这不是h(这不会匹配“p”本身)

p(?!h) I LIKE THIS ONE BEST as it uses the look-ahead feature of Regex. It says p, is it followed by an 'h'? No? That's good then.

p(?!h)我最喜欢这个,因为它使用Regex的先行功能。它说p,后面是h吗?没有?这很好。

Good luck.

祝你好运。

#3


0  

This won't match any word that contains "ph" or does not contain "p".

这将不匹配包含“ph”或不包含“p”的任何单词。

\b(?:p(?!h)|[^p\s])*?p(?!h)(?:p(?!h)|[^p\s])*?\b

#4


0  

I am giving you executable code, I interchange the sequence of !w.includes('ph') & w.includes('p'). And javascript execution of statment

我给你的是可执行代码,我交换的顺序是!w包括('ph')和w包括('p')。和javascript执行的状态。

var str = 'python php ruby javascript jsonp perhapsphpisoutdated';
var res=str.split(' ').filter( w=> !w.includes('ph') && w.includes('p') )
console.log(res);

#1


9  

You may use the following solution:

您可以使用以下解决方案:

var str = 'python php ruby javascript jsonp perhapsphpisoutdated';
var res = str.match(/\b(?!\w*ph)\w*p\w*/g);
console.log(res);

Description:

描述:

  • \b - leading word boundary
  • \b -引导词边界。
  • (?!\w*ph) - a negative lookahead requiring that there cannot be ph after 0+ word chars that appear right after the leading word boundary
  • (? \w*ph) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  • \w* - 0+ word chars
  • \w* - 0+单词chars。
  • p - a letter p
  • p -字母p。
  • \w* - 0+ word chars.
  • \w* - 0+单词chars。

Note that \w matching letters, digits and underscores can be replaced with [a-zA-Z] to only match ASCII letters.

注意,可以用[a-zA-Z]替换字母、数字和下划线来匹配ASCII字母。

Visualization:

可视化:

找到所有包含“p”但不是“ph”的词

#2


1  

This is actually something you could do a number of different ways. I suggest you use https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ and look at the quantifiers and matching. You might also read up on look ahead http://www.regular-expressions.info/lookaround.html

这实际上是你可以用不同的方法做的事情。我建议你使用https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/,查看量词和匹配。你也可以阅读http://www.regular-expressions.info/lookaround.html。

Here are a couple of ideas.

这里有一些想法。

p{1}h{0} this says exactly 1 p followed by exactly 0 h

p{1}h{0}正好是1 p,后面正好是0 h。

p[^h] this says p followed by anything which is not h (this won't match "p" by itself)

h p[^]随后p说什么这不是h(这不会匹配“p”本身)

p(?!h) I LIKE THIS ONE BEST as it uses the look-ahead feature of Regex. It says p, is it followed by an 'h'? No? That's good then.

p(?!h)我最喜欢这个,因为它使用Regex的先行功能。它说p,后面是h吗?没有?这很好。

Good luck.

祝你好运。

#3


0  

This won't match any word that contains "ph" or does not contain "p".

这将不匹配包含“ph”或不包含“p”的任何单词。

\b(?:p(?!h)|[^p\s])*?p(?!h)(?:p(?!h)|[^p\s])*?\b

#4


0  

I am giving you executable code, I interchange the sequence of !w.includes('ph') & w.includes('p'). And javascript execution of statment

我给你的是可执行代码,我交换的顺序是!w包括('ph')和w包括('p')。和javascript执行的状态。

var str = 'python php ruby javascript jsonp perhapsphpisoutdated';
var res=str.split(' ').filter( w=> !w.includes('ph') && w.includes('p') )
console.log(res);