如何将此正则表达式修改为与Python中的“&”不匹配?

时间:2022-12-01 16:49:59

Background

I would like to extract a piece of information out of a string. For example,

我想从字符串中提取一条信息。例如,

NO PARKING (SANITATION BROOM SYMBOL) 8-9:30AM TUES & FRI

禁止停车(卫生布鲁姆符号)8-9:30 AM TUES&FRI

Because it's only TUES and FRI. It's not a range of days so I would like to just get ['TUES', 'FRI']

因为它只是TUES和FRI。这不是一个天的范围,所以我想得到['TUES','FRI']

However, for something like

但是,对于类似的东西

2 HOUR PARKING 9AM-5PM MON THRU SAT

It would be a range of days so the result would be ['MON', 'TUES', 'WED', 'THURS', 'FRI', 'SAT']

这将是一段天,所以结果将是['MON','TUES','WED','THURS','FRI','SAT']

I have this regex which works with a range of days but I would like to modify to except just one rule.

我有这个正则表达式适用于一系列的日子,但我想修改为除了一个规则。

\b((?:(?:MON|MONDAY|TUES|TUESDAY|WED|WEDNESDAY|THURS|THURSDAY|FRI|FRIDAY|SAT|SATURDAY|SUN|SUNDAY)\s*)+)(?=\s|$)

It works with

它适用于

NO STANDING 11AM-7AM MON SAT

which extracts MON and SAT. But I don't want it to match

提取MON和SAT。但我不希望它匹配

NO PARKING (SANITATION BROOM SYMBOL) 8-9:30AM TUES & FRI

Because I have another regex which I want it to be matched.

因为我有另一个正则表达式,我希望它匹配。

1 个解决方案

#1


2  

If you have many regex I would suggest running them in specific order (give them a priority) such that the more specific regex / more important ones will match first. That way you could stop matching if one regex matched, in your case the "other" regex.

如果你有很多正则表达式,我会建议按特定顺序运行它们(给它们一个优先级),这样更具体的正则表达式/更重要的正则表达式将首先匹配。这样你可以停止匹配,如果一个正则表达式匹配,在你的情况下是“其他”正则表达式。

#1


2  

If you have many regex I would suggest running them in specific order (give them a priority) such that the more specific regex / more important ones will match first. That way you could stop matching if one regex matched, in your case the "other" regex.

如果你有很多正则表达式,我会建议按特定顺序运行它们(给它们一个优先级),这样更具体的正则表达式/更重要的正则表达式将首先匹配。这样你可以停止匹配,如果一个正则表达式匹配,在你的情况下是“其他”正则表达式。