【发布时间】:2018-09-10 18:11:02
【问题描述】:
我想为grep 编写一个灵活的正则表达式,它将返回在一定距离内找到的搜索词。
理想的行为类似于研究数据库;例如,您可以在其中搜索 capital 和 GDP 彼此相距在 15 个字以内的文章,其中包括字符串 capital 和 GDP 可能被五、六、七等分隔的文章.,未指定长度的字母数字字符串。正则表达式语句将包括标点符号(例如,逗号、句号、连字符),但也包括重音符号和变音符号。因此,chechè 和 lavi 的结果相隔不超过五个字符串。
我想该声明将涉及前瞻,以及像{1,15} 这样的短语,或者可能将一个grep 传递到另一个grep,但这失去了GREP_OPTIONS='--color=auto' 的好处。构建它真的超出了我的技能范围。我有一组 .txt 文档,我想对其进行搜索,但是让正则表达式灵活地更改字符串之间的距离或截断术语对于在标准中有 fieldnotes 或阅读笔记等内容的其他人也很有用格式。
编辑
以下是从圣经中摘录的一段示例。
Ye shall buy meat of them for money, that ye may eat; and ye shall also buy water of them for money, that ye may drink. For the Lord thy God hath blessed thee in all the works of thy hand: he knoweth thy walking through this great wilderness: these forty years the Lord thy God hath been with thee; thou hast lacked nothing... Thou shalt sell me meat for money, that I may eat; and give me water for money, that I may drink: only I will pass through on my feet: (as the children of Esau which dwell in Seir, and the Moabites which dwell in Ar, did unto me:) until I shall pass over Jordan into the land which the Lord our God giveth us. But Sihon king of Heshbon would not let us pass by him: for the Lord thy God hardened his spirit, and made his heart obstinate, that he might deliver him into thy hand, as appeareth this day. And the Lord said unto me, Behold, I have begun to give Sihon and his land before thee: begin to possess, that thou mayest inherit his land. Then Sihon came out against us, he and all his people, to fight at Jahaz. And the Lord our God delivered him before us; and we smote him, and his sons, and all his people. And if the way be too long for thee, so that thou art not able to carry it; or if the place be too far from thee, which the Lord thy God shall choose to set his name there, when the Lord thy God hath blessed thee: then shalt thou turn it into money, and bind up the money in thine hand, and shalt go unto the place which the Lord thy God shall choose: and thou shalt bestow that money for whatsoever thy soul lusteth after, for oxen, or for sheep, or for wine, or for strong drink, or for whatsoever thy soul desireth: and thou shalt eat there before the Lord thy God, and thou shalt rejoice, thou, and thine household, and the Levite that is within thy gates; thou shalt not forsake him: for he hath no part nor inheritance with thee... Now it came to pass, that at what time the chest was brought unto the king’s office by the hand of the Levites, and when they saw that there was much money, the king’s scribe and the high priest’s officer came and emptied the chest, and took it, and carried it to his place again. Thus they did day by day, and gathered money in abundance. And when they had finished it, they brought the rest of the money before the king and Jehoiada, whereof were made vessels for the house of the Lord , even vessels to minister, and to offer withal, and spoons, and vessels of gold and silver. And they offered burnt offerings in the house of the Lord continually all the days of Jehoiada. Thou hast bought me no sweet cane with money, neither hast thou filled me with the fat of thy sacrifices; but thou hast made me to serve with thy sins, thou hast wearied me with thine iniquities... Howbeit there were not made for the house of the Lord bowls of silver, snuffers, basins, trumpets, any vessels of gold, or vessels of silver, of the money that was brought into the house of the Lord: but they gave that to the workmen, and repaired therewith the house of the Lord. Moreover they reckoned not with the men, into whose hand they delivered the money to be bestowed on workmen: for they dealt faithfully. The trespass money and sin money was not brought into the house of the Lord: it was the priests’.
如果我想查找 shalt 和 money 在五个单词(包括标点符号)内同时出现的实例,我将如何编写该正则表达式?
我不确定如何给出预期的结果,因为grep --context=1 不仅仅包含中间有 0-5 个字符串的字符串,但我想结果会识别:
shalt sell me meat for money
shalt thou turn it into money
money in thine hand, and shalt
shalt bestow that money
但不会返回 shall buy meat of them for money,,因为 'money' 出现在第六个字符串中。
【问题讨论】:
-
在预期输出中包含您的示例输入
-
@anubhava 添加了示例文本
-
给出了预期的输出@EdMorton
-
wrt
I want to write a flexible regex for grep- 这真的是您想要的,还是您只是想要一个明智的解决方案来解决您的问题? -
@EdMorton 我确实想要一个明智的解决方案,但它应该足够灵活,可以轻松更改单词接近度或截断所需的更改,所以我觉得 grepping 是那个选项。我当然愿意接受更明智的选择。