簡體   English   中英

使用正則表達式查找 substring

[英]using regex to find substring

我正面臨正則表達式使用的問題。 我正在使用以下正則表達式:

\\S*the[^o\\s]*(?<!theo)\\b

我使用的句子是:

如果全世界都說 theo 不是 oreo cookies 那么thetatheoder theotatheder thetatheder 是非常好的。

我想從 output 得到模式:然后,thetatheder,extratheaterly?

所以簡而言之,我可以將“the(The)”作為一個完整的字符串,或者將 substring 放在一個不包含“theo”的字符串中。

如何修改我的正則表達式來實現這一點? 我想的是申請,pipe 操作還是問號。 但它們似乎都不可行。

通用的

如果你想設計一個通用的表達式,也許你可以從一些類似的表達式開始,

\S*the[^o\s]*\b

取決於你想匹配和不匹配,我猜。

演示

非通用

我想您可以簡單地找到有助於解決您的問題的單詞邊界( \b ),使用類似於以下的簡單表達式,

\b[Tt]he\b|\b[Tt]hen\b|\bextratheaterly\b

演示 1

或者,

\b(?:[Tt]hen?|[Ee]xtratheaterly)\b

演示 2

Java 測試

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex = "\\b(?:[Tt]hen?|[Ee]xtratheaterly)\\b";
        final String string = "If the world says that theo is not oreo cookies then thetatheoder is extratheaterly good.\n\n"
             + "If The world says that theo is not oreo cookies Then thetatheoder is Extratheaterly good.\n\n"
             + "If notthe world says that theo is not oreo cookies notthen thetatheoder is notextratheaterly good.\n\n\n";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }


    }
}

Output

Full match: the
Full match: then
Full match: extratheaterly
Full match: The
Full match: Then
Full match: Extratheaterly

Python 測試

import re
string = '''
If the world says that theo is not oreo cookies then thetatheoder is extratheaterly good.

If The world says that theo is not oreo cookies Then thetatheoder is Extratheaterly good.

If notthe world says that theo is not oreo cookies notthen thetatheoder is notextratheaterly good.
'''

expression = r'\b(?:[Tt]hen?|[Ee]xtratheaterly)\b'

print(re.findall(expression, string))
print([m.group(0) for m in re.finditer(expression, string)])

Output

['the', 'then', 'extratheaterly', 'The', 'Then', 'Extratheaterly']
['the', 'then', 'extratheaterly', 'The', 'Then', 'Extratheaterly']

如果您想簡化/修改/探索表達式,它已在regex101.com的右上角面板上進行了解釋。 如果您願意,您還可以在此鏈接中觀看它如何與一些示例輸入匹配。


正則表達式電路

jex.im可視化正則表達式:

在此處輸入圖像描述

\b[A-Za-z]*he([a-z](?<!theo))*\b

匹配,然后,在劇院外

\b 字邊界

[A-Za-z] 匹配任何字母

[az] 匹配任何小寫字母

* 匹配 0 個或更多

([a-z](?<!theo))*

這是棘手的部分。 它說任何字母,確保在添加該字母后它不拼寫 theo(向后看)

看看消極的后視和消極的前瞻。

您可以在否定的lookbehind 中使用\S作為起始邊界和否定的lookahead,以確保單詞不包含theo。

要匹配 The 或 the 您可以使模式不區分大小寫。

(?<!\S)(?!\S*theo\S*)\S*the\S*

在零件

  • (?<!\S)否定后視,斷言左邊的不是非空白字符
  • (?!\S*theo\S*)負前瞻,斷言右邊的內容不包含theo
  • \S*the\S* the 0+ 次非空白字符包圍的匹配

正則表達式演示

如果您只使用單詞字符,您還可以使用單詞邊界\b

\b(?!\w*theo\w*)\w*the\w*\b

正則表達式演示

或者你可以斷言單詞的一部分是the並使用斷言匹配它,如果你匹配一個t它不應該跟heo

\b(?=\S*the\S*)[^t\s]*(?:t(?!heo)[^t\s]*)+\b

正則表達式演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM