寻找两个相邻的字词寻找两个相邻的字词寻找两个相邻的字词寻找两个相邻的字词
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
寻找两个相邻的字词
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 寻找两个相邻的字词

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效日期
数字日期转换为文本
信用卡号码
比对整行
删除重复列
编程
两个相邻的字词
陷阱
灾难性的回溯
过多重复
拒绝服务
让所有内容都可选
重复捕获组
混合 Unicode 和 8 比特
本网站上的更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

寻找两个相邻的字词

有些使用布尔操作符的搜索工具也有一个称为「相邻」的特殊操作符。搜索「字词1 相邻 字词2」会找出所有在彼此「距离」范围内出现字词1 和字词2 的结果。距离是字词数。实际数字取决于搜索工具,而且通常可以设置。

您可以轻松地使用适当的正则表达式运行相同的任务。

使用正则表达式仿真「相邻」

使用正则表达式,你可以描述几乎任何文本模式,包括符合两个相邻单字的模式。这个模式相对简单,由三部分组成:第一个单字、一定数量的未指定单字,以及第二个单字。未指定的单字可以用 简写字符类别 \w+ 来符合。单字之间的空格和其他字符可以用 \W+ 来符合(这次是大写的 W)。

完整的正则表达式变成 \bword1\W+(?:\w+\W+){1,6}?word2\b。量词 {1,6}? 使得正则表达式要求「word1」和「word2」之间至少有一个单字,最多允许六个单字。

如果这些单字也可能以相反的顺序出现,我们需要另外指定相反的模式

\b(?:word1\W+(?:\w+\W+){1,6}?word2|word2\W+(?:\w+\W+){1,6}?word1)\b

如果你想从一个单字清单中找出任何两个单字的配对,你可以使用

\b(word1|word2|word3)(?:\W+\w+){1,6}?\W+(word1|word2|word3)\b

最后的正则表达式也会找出一个单字附近的单字。例如,它会符合 word2 near word2。

尋找兩個相鄰的字詞
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 尋找兩個相鄰的字詞

範例
正規表示式範例
數字範圍
浮點數
電子郵件地址
IP 地址
有效日期
數字日期轉換為文字
信用卡號碼
比對整行
刪除重複列
程式設計
兩個相鄰的字詞
陷阱
災難性的回溯
過多重複
拒絕服務
讓所有內容都可選
重複擷取群組
混合 Unicode 和 8 位元
本網站上的更多內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

尋找兩個相鄰的字詞

有些使用布林運算子的搜尋工具也有一個稱為「相鄰」的特殊運算子。搜尋「字詞1 相鄰 字詞2」會找出所有在彼此「距離」範圍內出現字詞1 和字詞2 的結果。距離是字詞數。實際數字取決於搜尋工具,而且通常可以設定。

您可以輕鬆地使用適當的正規表示式執行相同的任務。

使用正規表示式模擬「相鄰」

使用正規表示法,你可以描述幾乎任何文字模式,包括符合兩個相鄰單字的模式。這個模式相對簡單,由三部分組成:第一個單字、一定數量的未指定單字,以及第二個單字。未指定的單字可以用 簡寫字元類別 \w+ 來符合。單字之間的空格和其他字元可以用 \W+ 來符合(這次是大寫的 W)。

完整的正規表示法變成 \bword1\W+(?:\w+\W+){1,6}?word2\b。量詞 {1,6}? 使得正規表示法要求「word1」和「word2」之間至少有一個單字,最多允許六個單字。

如果這些單字也可能以相反的順序出現,我們需要另外指定相反的模式

\b(?:word1\W+(?:\w+\W+){1,6}?word2|word2\W+(?:\w+\W+){1,6}?word1)\b

如果你想從一個單字清單中找出任何兩個單字的配對,你可以使用

\b(word1|word2|word3)(?:\W+\w+){1,6}?\W+(word1|word2|word3)\b

最後的正規表示法也會找出一個單字附近的單字。例如,它會符合 word2 near word2。

Find Two Words Near Each Other
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Find Two Words Near Each Other

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Find Two Words Near Each Other

Some search tools that use boolean operators also have a special operator called “near”. Searching for “term1 near term2” finds all occurrences of term1 and term2 that occur within a certain “distance” from each other. The distance is a number of words. The actual number depends on the search tool, and is often configurable.

You can easily perform the same task with the proper regular expression.

Emulating “near” with a Regular Expression

With regular expressions you can describe almost any text pattern, including a pattern that matches two words near each other. This pattern is relatively simple, consisting of three parts: the first word, a certain number of unspecified words, and the second word. An unspecified word can be matched with the shorthand character class \w+. The spaces and other characters between the words can be matched with \W+ (uppercase W this time).

The complete regular expression becomes \bword1\W+(?:\w+\W+){1,6}?word2\b. The quantifier {1,6}? makes the regex require at least one word between “word1” and “word2”, and allow at most six words.

If the words may also occur in reverse order, we need to specify the opposite pattern as well:

\b(?:word1\W+(?:\w+\W+){1,6}?word2|word2\W+(?:\w+\W+){1,6}?word1)\b

If you want to find any pair of two words out of a list of words, you can use:

\b(word1|word2|word3)(?:\W+\w+){1,6}?\W+(word1|word2|word3)\b

The final regex also finds a word near itself. It will match word2 near word2, for example.

©2015-2025 艾丽卡 support@alaica.com