如何使用正则表达式排序?

时间:2019-06-07 14:56:24

标签: regex regex-lookarounds

/(summer camp)|(summer)|(camp)/
Forest camp
summer ramadan
summer camp
summer rakatun

我在“夏令营”,“夏令营”,“夏令营”链接上有测试数据,我需要做一个正则表达式,以便“夏令营”成为第一个匹配项

regexp具有比赛的搜索顺序,尽管排在第三位,但我需要第一场比赛才能找到夏令营。但现在是第一个“训练营”比赛。

https://regex101.com/r/YWgrlO/1/

1 个答案:

答案 0 :(得分:2)

regexp has a search order of matches, I need the first match to find a summer camp, despite the fact that it is third on the list. But now the first "camp" match.

不确定您的意思,但是如果您想按优先顺序查找它们
首先找到夏令营的序列,就像这样

(?s)(summer camp)(?:.+?(summer|camp)?)?

https://regex101.com/r/0A7TBD/1

相关问题