- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设这是我们的文本:
text = 'After 1992 , the winter and summer Olympics will be held two years apart , with the revised schedule beginning with the winter games in 1994 and the summer games in 1996 . ) Now , Mr. Pilson -- a former college basketball player who says a good negotiator needs `` a level of focus and intellectual attention similar to a good athlete-s is facing the consequences of his own aggressiveness . Next month , talks will begin on two coveted CBS contracts'
print re.search(r'(\w+ |\W+ ){0,4}1992( \W+| \w+){4}', text).group(0)
Output: After 1992 , the winter and
但是这个给了我:
print re.search(r'(\w+ |\W+ ){0,4}1992( \W+| \w+){0,4}', text).group(0)
Output: After 1992 ,
我觉得很奇怪,为什么第二个正则表达式不贪心?
这个比其他的有点奇怪:
print re.search(r'(\w+ |\W+ ){0,4}summer( \W+| \w+){0,4}', text).group(0)
Output , the winter and summer Olympics will be held
问题
1-第一个和第二个有什么区别。对我来说,它应该给出相同的文本,因为唯一的区别是 {0,4}
并且如果 {4}
给出长字符串,{0,4}
应该给出相同的字符串,因为正则表达式是贪婪的。
2- 问题可能与标点符号有关,因为第三个示例在 {0,4}
和 {4}
上工作相同..
我很困惑。
最佳答案
这里没有什么神秘的。
在您的第二个示例中,␣\W+
超过了 ␣,␣
(空白 ␣
也是 \W< 的一部分
类),因此未找到 ␣\w+
与剩余的 the␣winter␣...
的后续匹配项——但是 {0, 4}
约束得到满足,所以不需要那些进一步的匹配。到目前为止一切顺利。
回到你的第一个例子,上面的匹配不满足 {4}
,所以引擎继续寻找。在 ␣\W+
匹配中它回溯了最后一个空白 ␣
所以 ␣\W+
只匹配了 ␣,
, 然后␣\w+
的 3 个后续匹配可以针对 ␣the␣winter␣...
-- 并且 {4}
得到满足。
将您的正则表达式更改为 ([^ ]+ +){0,4}my_word( +[^ ]+){0,4}
(这保持了原始表达式的精神,将空格视为分隔符,将其他所有内容(包括标点符号)视为单词)或者,也许更好,(\w+\W+){0,4}my_word(\W+\w+){0,4}
无论标点符号如何,在两边最多隔离 4 个实际单词。
稍后,
Hi vladr. Regular expression that you provided is not working with this text (target word is part in this text):
The city 's Department of Consumer Affairs charged Newmark & Lewis Inc. with failing to deliver on its promise of lowering prices . In a civil suit commenced in state Supreme Court in New York , the agency alleged that the consumer-electronics and appliance discount-retailing chain engaged in deceptive advertising by claiming to have '' lowered every price on every item '' as part of an advertising campaign that began June 1 . The agency said it monitored Newmark & Lewis 's advertised prices before and after the ad campaign , and found that the prices of at least 50 different items either increased or stayed the same . In late May , Newmark & Lewis announced a plan to cut prices 5 % to 20 % and eliminate what it called a '' standard discount-retailing practice '' of negotiating individual deals with customers ."
啊哈。它在 Department 中匹配 part。
(^|(\w+\W+){1,5})\W*my_word\W*((\W+\w+){1,5} |$)
,这应该隔离分隔符和/或行尾之间的单词。(\w+\W+){0,5}\w*my_word\w*(\W*\w+){0,5}
关于python - 正则表达式 : Punctuation and greediness,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13349946/
import urllib2,sys from bs4 import BeautifulSoup,NavigableString from string import punctuation as p
假设这是我们的文本: text = 'After 1992 , the winter and summer Olympics will be held two years apart , with t
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
有人知道从 SQL Server 字段中删除标点符号的好方法吗? 我在想 UPDATE tblMyTable SET FieldName = REPLACE(REPLACE(REPLACE(Field
我感觉标点符号能够访问本地存储数据,而 Java KafkaStreams 库正在重建它并重播更改日志主题中的项目 让我们描述一下我们的场景:我有一个 KafkaStreams 应用程序 A(用 Ja
我有以下接口(interface)实现: package io.khinkali.topology; import org.apache.kafka.streams.processor.*; publ
我很困惑为什么这段代码不能按我想要的方式工作。我正在读取 txt 文件并将每个项目(逗号分隔)打印到新行上。每个项目都用“”包围,并且还包含标点符号。我正在尝试删除这个标点符号。我熟悉 string.
我想检查句子结构的质量。具体来说,我想看看最终用户是否在标点符号后输入空格。我也可以使用 NLP 库或简单的 java 正则表达式解决方案。 例如: “嗨,我叫汤姆·克鲁斯。我喜欢电影” “嗨,我叫汤
我正在寻找诸如之类的句子 Bachelors Degree in early childhood teaching, psychology 我使用斯坦福解析器对文本进行注释。 然后,我迭代每个句子并使
我有一个字符串,我想用它删除所有标点符号。我目前使用: import string translator = str.maketrans('','', string.punctuation) name
print string.punctuation 看起来像这样: !"#$%&'()*+,-./:;?@[\]^_`{|}~ 我想知道我们是否可以在其中添加更多标点符号,例如像这样的中文句号:“。”
我正在使用阿拉伯语文本,我想删除阿拉伯语标点符号示例: s="أهلاً بالعالم في هذه التجربة ! علامات ،الترقيم ؟ ,? لا .اتذكرها" 我希望输
我正在尝试查找句子中的最后一个标点符号或空格字符。 鉴于我有这句话,"Hello! What is your name?" 我希望正则表达式返回 ? 的索引, 但我的正则表达式返回 ! 的索引 我的尝
我正在将数据从 SQL Server 表导出到 .csv 文件,然后我使用 sp_send_email 将数据通过电子邮件发送给该文件。 我的问题是这个值: Cantata Number 212 "P
在流应用程序中的单个任务中,以下两个方法是否独立运行(意味着“process”方法正在处理来自上游源的传入消息,“punctuate”方法也可以根据指定的时间表并行运行,并且WALL_CLOCK_TI
我试图弄清楚如何打开一个文件,将文件中的所有字母变成小写,然后取出所有标点符号。我尝试了一些在网上和书中看到的东西,但我似乎无法弄清楚。 import string def ReadFile(File
根据各种documentation ,要匹配任何标点符号我需要使用“\p{P}”模式 #include #include ... std::string str = "Hello'\"#%&!.:
我正在尝试使用 javascript 的 RegExp 来匹配完整的单词,但是当这些单词以标点符号作为边界时它不起作用。 IE。 (new RegExp("\\b"+RegExp.escape("wh
我正在尝试将旧的 phpBB 论坛数据库移动到新服务器。因此,当我下载 .sql 文件时,我尝试通过 phpMyAdmin 面板将其上传到新数据库。这是我收到的错误: Error There seem
How????are!!!you 我想将字符串拆分为 ['How','are','you']。 我试过以下正则表达式: \?*|\!* 这是行不通的。但是,以下正则表达式有效: \?+|\!+ 有人给
我是一名优秀的程序员,十分优秀!