正则表达式:匹配包含某些单词的所有单词

时间:2022-09-13 09:19:30

I want to match all the words that contains the word "oana". I put "OANA" with uppercase letters in some words, at the beginning, middle, and at the end of words.

我想匹配包含单词“oana”的所有单词。我在一些单词中,在单词的开头,中间和末尾加上带有大写字母的“OANA”。

blah OANAmama blah aOANAtata aOANAt msmsmsOANAasfasfa mOANAmsmf OANAtata OANA3 oanTy

blah OANAmama blah aOANAtata aOANAt msmsmsOANAasfasfa mOANAmsmf OANAtata OANA3 oanTy

Anyway, I made a regex, but it is not very good, because it doesn't select all words that contains "oana"

无论如何,我做了一个正则表达式,但它不是很好,因为它没有选择包含“oana”的所有单词

\b\w+(oana)\w+\b

Can anyone give me another solution?

谁能给我另一种解决方案?

1 个解决方案

#1


2  

You need to use a case insensitive flag and replace + with *:

您需要使用不区分大小写的标志并将+替换为*:

/\b\w*oana\w*\b/i

See the regex demo (a global modifier may or may not be used, depending on the regex engine). The case insensitive modifier may be passed as an inline option in some regex engines - (?i)\b\w*oana\w*\b.

请参阅正则表达式演示(可以使用或不使用全局修饰符,具体取决于正则表达式引擎)。不区分大小写的修饰符可以在一些正则表达式引擎中作为内联选项传递 - (?i)\ b \ w * oana \ w * \ b。

Here,

  • \b - a word boundary
  • \ b - 单词边界

  • \w* - 0+ word chars
  • \ w * - 0+单词字符

  • oana - the required char string inside a word
  • oana - 单词中所需的char字符串

  • \w* - 0+ word chars
  • \ w * - 0+单词字符

  • \b - a word boundary
  • \ b - 单词边界

#1


2  

You need to use a case insensitive flag and replace + with *:

您需要使用不区分大小写的标志并将+替换为*:

/\b\w*oana\w*\b/i

See the regex demo (a global modifier may or may not be used, depending on the regex engine). The case insensitive modifier may be passed as an inline option in some regex engines - (?i)\b\w*oana\w*\b.

请参阅正则表达式演示(可以使用或不使用全局修饰符,具体取决于正则表达式引擎)。不区分大小写的修饰符可以在一些正则表达式引擎中作为内联选项传递 - (?i)\ b \ w * oana \ w * \ b。

Here,

  • \b - a word boundary
  • \ b - 单词边界

  • \w* - 0+ word chars
  • \ w * - 0+单词字符

  • oana - the required char string inside a word
  • oana - 单词中所需的char字符串

  • \w* - 0+ word chars
  • \ w * - 0+单词字符

  • \b - a word boundary
  • \ b - 单词边界