简体   繁体   English

如何使用正则表达式词

[英]How to use regex for words

I was trying a simple regex code to match the following: 我正在尝试一个简单的正则表达式代码来匹配以下内容:

line = 'blah black blacksheep blah' 
if re.match(r'(\bblack\b)', line):
    print 'found it!

What am I doing wrong, I can't find "black" by itself? 我做错了什么,我自己找不到“黑色”?

From the docs : 文档

re.match(pattern, string, flags=0)

If zero or more characters at the beginning of string match the regular expression pattern , return a corresponding MatchObject instance. 如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的MatchObject实例。

You probably want to use re.search or re.findall instead. 您可能想改用re.searchre.findall

You should use re.search or re.findall here: 您应该在此处使用re.searchre.findall

>>> strs = 'blah black blacksheep blah'
>>> re.search(r'\bblack\b', strs).group(0)
'black'

>>> re.findall(r'\bblack\b', strs)
['black']

You want re.search instead of re.match . 您需要re.search而不是re.match From the docs : 文档

7.2.5.3. 7.2.5.3。 search() vs. match() search()与match()

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default). Python提供了两种基于正则表达式的原始操作: re.match()仅在字符串的开头检查匹配项,而re.search()在字符串的任何位置检查匹配项(这是Perl的默认设置) )。

Use re.search() (returns None if no match on the whole string): 使用re.search() (如果整个字符串不匹配,则返回None):

line = 'blah black blacksheep blah' 
if re.search(r'(\bblack\b)', line):
    print 'found it!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免需要递归使用正则表达式删除字符串末尾的单词? - How to avoid needing to use regex recursively to remove words at the end of a string? 如何使用正则表达式删除带有数字的特定单词模式? - How to use regex to remove a particular pattern of words with numbers? 如何在python中使用regex模块将文本字符串拆分为仅单词? - How to use the regex module in python to split a string of text into the words only? 如何使用正则表达式查找带有句点分隔符的组合词? - How to use a regex to find combined words with period delimiters? 如何使用正则表达式仅保留前n个重复的单词 - How to use regex to only keep first n repeated words 如何使用正则表达式在PDF中搜索括号内的所有单词除了特定的单词集外? - How can I use a regex to search a PDF for all words within parentheses EXCEPT for a specific set of words? 如何使用正则表达式将单词列表与另一个单词列表进行比较并打印匹配项? - How can I use regex to compare a list of words with another list of words and print the matches? 如何在正则表达式中包含重音词 - How to Include accented words in regex 正则表达式-如何捕获许多单词 - Regex - how to capture many words 如何处理正则表达式中的复合词 - how to deal with compound words in regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM