繁体   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