我正在尝试在一个天气软件中设置一个警报,以查看我所在地区的天气预报,并告诉我在即将到来的天气预报中是否出现“严重”一词。我正在查看以下文本文件(缩短了一点):
000
FPUS55 KBOU 301529
ZFPBOU
Zone Forecast Product for Northeast Colorado
National Weather Service Denver/Boulder CO
929 AM MDT Sat Jun 30 2018
COZ042-044-010615-
Northeast Weld County-Morgan County-
including Briggsdale, Grover, Pawnee Buttes, Raymer, Stoneham,
Brush, Fort Morgan, Goodrich, and Wiggins
929 AM MDT Sat Jun 30 2018
.REST OF TODAY...Chance of thunderstorms early in the afternoon.
Thunderstorms likely late in the afternoon. Some thunderstorms
may be severe with large hail. Highs 68 to 74. Northeast winds 10
to 15 mph with gusts to around 25 mph. Chance of thunderstorms 70
percent.
.TONIGHT...Mostly cloudy with a 30 percent chance of
thunderstorms in the evening, then mostly clear after midnight.
Some thunderstorms may be severe. Lows near 50. North winds 10 to
15 mph with gusts to around 25 mph in the evening becoming light.
.SUNDAY...Mostly sunny. Warmer. Highs in the 80s.
.SUNDAY NIGHT...Mostly clear. Lows in the mid to upper 50s. South
winds 10 to 15 mph.
.MONDAY...Mostly sunny. Highs near 90.
.MONDAY NIGHT AND TUESDAY...Partly cloudy with a 10 percent
chance of thunderstorms. Lows near 60. Highs in the lower to mid
90s.
.TUESDAY NIGHT AND Independence Day...Partly cloudy. Lows near
60. Highs in the 90s.
.WEDNESDAY NIGHT AND THURSDAY...Partly cloudy with a 10 percent
chance of thunderstorms. Lows near 60. Highs in the lower to mid
90s.
.THURSDAY NIGHT...Partly cloudy with a 30 percent chance of
thunderstorms. Lows near 60.
.FRIDAY...Partly cloudy with a 10 percent chance of
thunderstorms. Highs in the lower to mid 90s.
$$
COZ048>051-010615-
Logan County-Washington County-Sedgwick County-Phillips County-
including Crook, Merino, Sterling, Peetz, Akron, Cope,
Last Chance, Otis, Julesburg, Ovid, Sedgwick, Amherst, Haxtun,
and Holyoke
929 AM MDT Sat Jun 30 2018
.REST OF TODAY...Chance of showers and slight chance of
thunderstorms early in the afternoon. Showers likely and chance
of thunderstorms late in the afternoon. Highs in the lower 70s.
North winds 10 to 20 mph. Chance of precipitation 60 percent.
.TONIGHT...Mostly cloudy with a 50 percent chance of
thunderstorms in the evening, then mostly clear after midnight.
Some thunderstorms may be severe. Lows in the lower to mid 50s.
North winds 10 to 15 mph with gusts to around 25 mph in the
evening becoming light.
.SUNDAY...Mostly sunny. Highs in the mid 80s.
.SUNDAY NIGHT...Mostly clear. Lows near 60. South winds 10 to
15 mph.
.MONDAY...Partly cloudy with a 10 percent chance of
thunderstorms. Highs in the lower 90s. South winds 10 to 15 mph.
.MONDAY NIGHT...Partly cloudy with a 10 percent chance of
thunderstorms. Lows near 60.
.TUESDAY...Partly cloudy. Highs in the mid 90s.
.TUESDAY NIGHT...Partly cloudy with a 10 percent chance of
thunderstorms. Lows in the lower to mid 60s.
.INDEPENDENCE DAY...Partly cloudy. Highs in the mid 90s.
.WEDNESDAY NIGHT...Partly cloudy with a 10 percent chance of
thunderstorms. Lows in the lower to mid 60s.
.THURSDAY...Partly cloudy with a chance of rain showers and
slight chance of thunderstorms. Highs in the lower 90s. Chance of
precipitation 30 percent.
.THURSDAY NIGHT...Partly cloudy with a 30 percent chance of
thunderstorms. Lows in the lower to mid 60s.
.FRIDAY...Partly cloudy. Highs in the lower 90s.
$$
COZ046-010615-
North and Northeast Elbert County Below 6000 Feet/North Lincoln
County-
including Agate, Hugo, Limon, and Matheson
929 AM MDT Sat Jun 30 2018
.REST OF TODAY...Mostly cloudy. Chance of rain showers and slight
chance of thunderstorms early in the afternoon. Chance of
thunderstorms late in the afternoon. Some thunderstorms may be
severe late in the afternoon. Highs in the mid 70s. North winds
15 to 25 mph. Chance of precipitation 40 percent.
.TONIGHT...Mostly cloudy with a 50 percent chance of
thunderstorms in the evening, then partly cloudy after midnight.
Lows around 50. North winds 10 to 20 mph in the evening becoming
light.
.SUNDAY...Mostly sunny. Highs in the lower 80s. South winds 10 to
15 mph in the afternoon.
.SUNDAY NIGHT...Partly cloudy with a 10 percent chance of
thunderstorms. Lows in the mid to upper 50s. South winds 10 to
15 mph.
.MONDAY...Partly cloudy with a 10 percent chance of
thunderstorms. Highs near 90. South winds 10 to 15 mph.
.MONDAY NIGHT...Partly cloudy with a 10 percent chance of
thunderstorms. Lows in the mid 50s to lower 60s.
.TUESDAY THROUGH INDEPENDENCE DAY...Partly cloudy. Highs in the
lower to mid 90s. Lows in the mid 50s to lower 60s.
.WEDNESDAY NIGHT...Mostly cloudy with a 20 percent chance of
thunderstorms. Lows near 60.
.THURSDAY...Partly cloudy with a 10 percent chance of
thunderstorms. Highs around 90.
.THURSDAY NIGHT...Partly cloudy with a 30 percent chance of
thunderstorms. Lows near 60.
.FRIDAY...Partly cloudy with a 10 percent chance of
thunderstorms. Highs in the upper 80s.
$$
因此,我想在小组内部寻找华盛顿县,这是上述预测的第二部分。短语“华盛顿县”将始终出现在我县的预测部分的标题中,“ $$”将始终结束预测的每个部分。例如,我发现RegEx表达式
Washington County([\D\S]*?)\${2}
将在我的预测部分中找到所有文本。然后,特别是在我县的天气预报范围内,我对“ TONIGHT”预报期感兴趣。我已经知道RegEx表达式
\.TONIGHT[\D\S]*?(?=\s\.)
将找到所有预测部分的全部的“ TONIGHT”预测期。当然,还有RegEx表达式
severe
将在整个文件中找到“严重”的所有实例。我遇到麻烦的地方是试图将这三个部分放在一起,仅在“华盛顿县”预测部分的“ TONIGHT”预测期内出现“严重”一词时才得出结果。当我尝试将所有这些放在一起时,我发现RegEx会找到我要查找的词,但是它将延伸到相邻的预测部分。有没有办法在“华盛顿县”和下一个“ $$”实例之间进行此搜索,以确保我不会溢出到下一个预测部分并返回假阳性?
非常感谢任何可以帮助我解决这个问题的人。我对RegEx还是很陌生,所以对于如何限制搜索范围我只是个好主意。
答案 0 :(得分:0)
您起步很好,但是在组合部分,您必须再编写两个RegEx并替换
[用于城市的正则表达式一] [用于TONIGHT的正则表达式二] [用于正则表达式的正则表达式3 严重]
使用
[用于城市的正则表达式一] [用于任何但没有城市的正则表达式一] [用于正则表达式二 TONIGHT] [对于新部分,另加一个] [RegEx 3:严重]
那是开始...
答案 1 :(得分:0)
您可以使用negative lookahead断言来实现所需的目标。
例如,
Ab(?!c).
匹配Ab
,后跟c
以外的任何字符
Ab((?!c).)+
匹配Ab
,然后继续匹配任何字符,直到命中c
对于您而言,除非我们在本节末尾的换行符上击$$
,否则我们希望保持匹配。为此,我们可以使用Washington County((?!\R\$\$)[\s\S])+
。 [\s\S]
可以匹配任何字符,但是(?!\R\$\$)
会强制其在遇到$$
时停止匹配。
稍微扩展一下这个概念,您只能在文本块的severe
部分中得出一个最终表达式来匹配.TONIGHT
。
Washington County((?!\R\$\$)[\s\S])+\R\.TONIGHT((?!\R\.)[\s\S])+severe
Washington County((?!\R\$\$)[\s\S])+\R\.TONIGHT
匹配华盛顿县街区中的所有东西,直到我们到达“ TONIGHT”部分。
((?!\R\.)[\s\S])+
从那时起一直保持匹配,直到我们遇到换行符并出现句号为止。这表示我们将离开“ TONIGHT”部分。我们需要正则表达式的这一部分来将查询限制为仅在TONIGHT部分中匹配,而不是超出该范围。
severe
在“ TONIGHT”部分中匹配“严重”。
答案 2 :(得分:0)
实际上,您可以将此文件分成用\n$$\n
分隔的块作为定界符。 sed
,awk
,perl
等中的任何一个都可以做到这一点,然后针对该块的简单正则表达式就可以满足您的要求。
awk
中的示例:
awk 'BEGIN {RS="\n\\$\\$\n"} /Washington County/ && /severe/ {print $0}' file
如果该块同时包含“华盛顿县”和“严重”,则将打印两个$$
之间的整个块。
如果您只想打印该部分的标题(位置)和特定时间(其中带有“ severe”),则可以进一步细分为以下部分:
awk 'BEGIN {RS="\n\\$\\$\n"; FS="\n\\."} /Washington County/ && /severe/
{print $1; for (i=1;i<=NF;i++) if(match($i, /severe/)) print $i}' file
打印:
COZ048> 051-010615-洛根县-华盛顿县-塞奇威克 菲利普斯郡县-包括克鲁克,美利奴羊,斯特林,佩兹, 阿克伦(Akron),应付(Cope),最后机会(Last Chance),奥的斯(Otis),儒勒斯堡(Julesburg),奥维德(Ovid),塞奇威克(Sedgwick),阿默斯特(Amherst), Haxtun和Holyoke 929 AM MDT星期六2018年6月30日
TONIGHT ...大部分多云,可能有50%的雷暴天气 晚上,午夜后大部分时间都晴朗。可能有雷雨 严厉。 50至50年代中期的低点。北风时速为10至15英里 傍晚阵风达到25英里每小时。