匹配"profile view"或"view_profile"而不是"profile.profile"或"view(view)"的最简洁方法是什么?

hen*_*tha 1 regex

我基本上正在寻找一个匹配这些的正则表达式:

ProfileView
profile_views
views of candidate profile
viewed_profile
Run Code Online (Sandbox Code Playgroud)

但不是这些:

profile(profile)
view = candidate.view
Run Code Online (Sandbox Code Playgroud)

换句话说,我想要a.+bb.+a不想a.+ab.+b.

我认为这需要以某种方式捕获组,但我不太确定如何.(a|b).+(^\1)......如果你理解我的意思,那就是这样的.

Avi*_*Raj 5

您需要使用交替运算符|.

(?i)^(?:profile.*views?|views?.*profile)$
Run Code Online (Sandbox Code Playgroud)

DEMO

s?匹配一个可选的s.(?i)不区分大小写的修饰符,它有助于进行不区分大小写的匹配.