Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不将单词边界括号与python正则表达式匹配_Python_Regex_Boundary - Fatal编程技术网

不将单词边界括号与python正则表达式匹配

不将单词边界括号与python正则表达式匹配,python,regex,boundary,Python,Regex,Boundary,我实际上有: regex = r'\bon the\b' 但仅当此关键字(实际上是“on the”)不在文本中的括号之间时,才需要匹配我的正则表达式: 应匹配: john is on the beach let me put this on the fridge he (my son) is on the beach arnold is on the road (to home) (my son is )on the beach john is at the beach bob is at

我实际上有:

 regex = r'\bon the\b'
但仅当此关键字(实际上是“on the”)不在文本中的括号之间时,才需要匹配我的正则表达式:

应匹配:

john is on the beach
let me put this on the fridge
he (my son) is on the beach
arnold is on the road (to home)
(my son is )on the beach
john is at the beach
bob is at the pool (berkeley)
the spon (is on the table)
不应匹配:

john is on the beach
let me put this on the fridge
he (my son) is on the beach
arnold is on the road (to home)
(my son is )on the beach
john is at the beach
bob is at the pool (berkeley)
the spon (is on the table)

在UNIX中,使用以下正则表达式的grep实用程序就足够了

grep " on the " input_file_name | grep -v "\(.* on the .*\)"

像这样的东西怎么样:
^(.*)(?:\(.*)(.*)(.*)$

按照您的要求,它“仅匹配文本中不在括号之间的单词”

因此,来自:

一些文本(括号中有更多文本)和一些不在括号中

匹配:
一些文本
+
和一些不在括号中

更多的例子在上面的链接


编辑:问题更改后更改答案

为了捕捉所有不在括号内的提及,我会使用一些代码,而不是一个巨大的正则表达式

像这样的东西会让你接近:

import re

pattern = r"(on the)"

test_text = '''john is on the bich
let me put this on the fridge
he (my son) is on the beach
arnold is on the road (to home)
(my son is )on the bitch
john is at the beach
bob is at the pool (berkeley)
the spon (is on the table)'''

match_list = test_text.split('\n')

for line in match_list:
    print line, "->",

    bracket_pattern = r"(\(.*\))" #remove everything between ()
    brackets = re.findall(bracket_pattern, line)
    for match in brackets:
        line = line.replace(match,"")

    matches = re.findall(pattern, line)
    for match in matches:
        print match

    print "\r"
输出:

john is on the bich -> on the
let me put this on the fridge -> on the
he (my son) is on the beach -> on the
arnold is on the road (to home) -> on the
(my son is )on the bitch -> on the (this in the only one that doesn't work)
john is at the beach -> 
bob is at the pool (berkeley) -> 
the spon (is on the table) -> 

我不认为正则表达式在一般情况下对你有帮助。 对于您的示例,此正则表达式将按照您的要求工作:

((?<=[^\(\)].{3})\bon the\b(?=.{3}[^\(\)])

希望这有帮助。

你的意思是说不在括号之间,也不直接在括号旁边?你试过什么了吗?括号是否总是平衡的?我在这里寻找像“on the”这样的特定单词,而不是所有不在括号中的单词。我将编辑我的问题以使其更清楚
mylist: a list contains all the lines you want to search through.
mystr: the string you want to find.