正则表达式在给定单词之前和之后搜索5个单词 [英] Regex to search for 5 words before and after a given word

查看:65
本文介绍了正则表达式在给定单词之前和之后搜索5个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个AS3程序,以在某些博客的rss中搜索某个关键字".

I need to write an AS3 program to search for a certain "keyword" in the rss of certain blogs.

我已经使用 String.indexOf()编写了逻辑,但这 EXTREMELY 很慢,而且不可扩展.我一直在寻找写一个正则表达式来查找关键字,但是在关键字之前和之后还返回5个单词(以显示搜索结果的上下文).

I've written logic using String.indexOf() but this is EXTREMELY slow, and not scalable. I've been looking to write a regular expression which looks for the keyword, but also returns 5 words before and after the keyword (to show the context of the search result).

我认为重叠的比赛可以忽略.

I suppose overlapping matches can be ignored.

我想出了(?:[a-zA-Z'-] + [^ a-zA-Z'-] +){0,5}关键字(?:[^ a-zA-Z'-] + [a-zA-Z'-] +){0,5}

唯一的问题是:它不查找完整单词.因此,在

The only problem with this is: It does not look for full words. So, using that regex on

那只敏捷的棕色狐狸跳过了那只懒狗,然后在狗窝里午睡了

the quick brown fox jumps over the lazy dog, and then goes for his afternoon nap in the kennel

对于关键字"the",将匹配

for the keyword "the", would match

快速的棕色狐狸跳过
懒狗和
n去狗窝午睡

the quick brown fox jumps over
the lazy dog, and the
n goes for his afternoon nap in the kennel

注意"then"被分割.

Notice the "then" gets split.

我怎么只匹配整个单词?

How do I match the whole word only?

我尝试在关键字之前和之后添加空格和标点符号,但这会在搜索文本的绝对开头和结尾出现关键字问题.

I tried adding whitespace and punctuation before and after the keyword, but that will cause a problem for keywords in the absolute start and end of the search text.

有什么办法解决吗?

将真实世界的文字设为

这些来自SCOR的新型楔形鞋提供不同程度的杆面倾角,当您接近果岭时,可以提供更高的准确性.对于大多数高尔夫球手来说,他们一半以上的投篮是在洞100码内进行的.这个数字大约占60%到65%,具体取决于您的游戏.尽管将球从发球区压倒很性感,但对我们大多数人来说,用手推杆或楔子变好会比对我们的得分增加15码的贡献更大.SCOR Golf最近发布了一个公司称为SCOR4161的系统.4161适用于它制作的球杆的可用杆面倾角,从41度一直到61度,以1度为增量.该范围允许高尔夫球手从一组铁杆中更换标准的9号铁杆和投球楔以及他们可能使用的其他楔形杆,以及一套设计用于130码及更近距离的精度的球杆.SCOR的说法是,通常我们用9铁和投球楔打的击球比5铁击球更像是沙楔击球;因此,将这些球杆设计得更像一个沙楔是有意义的.在测试中,我并不相信这个概念.通常情况下,我会全力击中9铁和PW.这些击球对感觉和技巧的需求并不相同,对我来说,我倾向于使用铁杆的球杆,而不是使用SCOR球杆.但是当我进入传统上被认为是楔形的三个鸽舍(49、54、59度)时,SCOR俱乐部给我留下了非常深刻的印象.摄影:Ariel Zambelich/Wired摄影有两点引人注目.传统上,楔形物在球杆上会产生一定量的反弹-后缘低于前缘.这样可以防止球杆在挥杆时掉入地下.通常,不同的谎言要求不同的弹跳角度:较高的弹跳表示沙子或较柔软的草皮,较低的弹跳表示更牢固,更紧的谎言.SCOR球杆具有反弹角度创新功能(该公司称为V-Sole),可让您在不同情况下使用每个球杆.球杆最前缘的弹跳角度很高-有时25度或更大-防止球杆挖入.但鞋底的其余部分以5到9的低弹跳进行磨光.度范围,在较硬的草皮上具有良好的性能.对我来说,结果是一个多才多艺的俱乐部,觉得它可以处理各种条件而没有太多问题.球杆不像传统的沙坑杆那样容易大开杆,所以开起来并不容易,但这远不止于它们在不同谎言中的一致性.其他体贴的内容比比皆是.球杆上的把手标记有拇指每隔一英寸的位置.这个想法是,您可以通过踩下球杆来稍微走一点,然后开始根据手的位置确切地了解击球的距离.SCOR甚至提供一本概述该方法的电子书,以及一个袋式标签,让您记下您的结果.这是更好地控制距离的好方法,这是取得好成绩的关键.总体而言,这里没有太多值得争论的地方.杆头柔软,虽然在我的测试过程中有些划痕,但感觉很好.杆头的设计经典且令人振奋,但杆身和握把上的图形有些分散注意力.低调一些的东西会很好.但是到目前为止,关于SCOR的最重要的事情是,它正在尝试构建一个系统,以帮助您从俱乐部到技术俱乐部的短暂比赛.大多数高尔夫球手凭感觉购买楔子-他们选择了一个杂色的小组,尝试了一些,看看他们如何击球.如果SCOR能让您考虑所有俱乐部如何共同影响您的比赛,那么它将为整个高尔夫界提供巨大的帮助.有线美丽的构建质量.各个俱乐部的感觉一致,各个条件下的表现一致.用户手册中的明智建议可帮助改善短杆比赛.小公司带来了出色的客户服务.疲倦如此微微的花哨设计.俱乐部每人$ 150,而五个俱乐部将使您退回$ 640.

These new wedges from SCOR offer varying degrees of loft, giving you more accuracy when you're close to the green. Photo by Ariel Zambelich/Wired For most golfers, well over half of their shots are played within 100 yards of the hole. That figure runs about 60 to 65 percent, depending on your game. While it’s sexy to crush the ball off the tee, for most of us, getting better with a putter or wedge in our hands would do much more for our scores than adding 15 yards to our drive. SCOR Golf recently released a system the company calls SCOR4161. The 4161 is for the available lofts of the clubs it makes, from 41 degrees of loft all the way up to 61 degrees, in one-degree increments. That’s a range that lets golfers replace their standard 9 iron and pitching wedge from their set of irons, as well as other wedges they might be using, with a set of clubs designed for precision from 130 yards and closer. SCOR’s claims here are that the shots we normally hit with a 9 iron and pitching wedge are much more like sand wedge shots than 5 iron shots; therefore, it makes sense to design those clubs to be more like a sand wedge. In my testing, I wasn’t as convinced of that notion. Usually, I’m hitting a 9 iron and PW more as a full shot. There’s not the same need for feel and finesse with those shots, and for me, I tended to be more consistent with the clubs from my iron set than with the SCOR clubs. But when I got to the three lofts that would be more traditionally thought of as wedges (49, 54, 59 degrees), I was very, very impressed with the SCOR clubs.Photo by Ariel Zambelich/Wired A couple of things stood out. Wedges traditionally include a certain amount of bounce on the club — the trailing edge is lower than the leading edge. This keeps the club from digging into the ground when you swing. Usually, different lies call for different bounce angles: higher bounce for sand or softer turf, lower bounce for firmer, tighter lies. The SCOR clubs include a bounce angle innovation (the company calls it V-Sole) that lets you use each of the clubs in different situations. The very leading edge of the club is ground with a very high bounce angle — sometimes 25 degrees or more — to keep the club from digging in. But the rest of the sole is ground at a much lower bounce, in the 5- to 9-degree range, which gives good performance on harder turf. The result for me was a versatile club that felt like it handled different conditions without much issue. The clubs aren’t quite as easy to open up as traditional sand wedges for big flop shots, but that was more than made up for by their consistency in different lies. Other thoughtful touches abound. The grips on the club come marked with places for your thumb spaced out every inch. The idea is that you can take a little distance off by choking down on the club, and begin to understand exactly how far you hit it based on your hand position. SCOR even provides an e-book outlining the method, and a bag tag that lets you jot down your results. It’s a great way to get better control over your distance, which is a key to good rounds. Overall, there’s not much to quibble with here. The clubheads are soft, which is good for feel, although they’ve gotten a little nicked up during my testing. The design of the clubhead is classic and confidence-inspiring, but the graphics on the shafts and the grips are a little distracting; something more understated would be nice. But by far the most important thing about SCOR is that it’s trying to build a system that helps your short game, from clubs to technique. Most golfers buy wedges by feel — they pick a motley group, trying out a few and seeing how they hit. If SCOR can get you to think about how all your clubs come together to influence your game, it will have done the whole golfing world a huge service. WIRED Beautiful build quality. Consistent feel across clubs, consistent performance across conditions. Sensible advice in owner’s manual to help improve short game. Small company leads to great customer service. TIRED Ever so slightly garish design. Clubs are $150 each, and five clubs will set you back $640.

推荐答案

所以在不到一个月的常规使用时间后,我发现了

So after a little less than a month of regular use, I find that

"((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}\W)(" + keyword + ")(\W(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5})"

效果最好.

我用单词边界代替了非单词字符.这样可以在关键字之前/之后立即使用标点符号.

I substituted the word boundaries for a non-word character. This allows for punctuation immediately before / after the keyword.

这篇关于正则表达式在给定单词之前和之后搜索5个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆