I have some text:
我有一些文字:
The great red fox. Which are not blue foxes. But foxes which are red are not any more faster.
伟大的红狐狸。这不是蓝狐。但是红色的狐狸不再快。
Basically I want to match sentences where "red" and "fox" both appear in that order and another regex where it is not in that order.
基本上我想匹配“红色”和“狐狸”都以该顺序出现的句子和另一个不按顺序出现的正则表达式。
How would I do that ?
我该怎么办?
3 个解决方案
#1
1
Assuming that there is no abbreviations with dots in your sentences:
假设句子中没有带点的缩写:
for any order:
对于任何订单:
(?=[^.!?]*fox)(?=[^.!?]*red)[^.!?]+[.!?]
for "red" before "fox":
对于“狐狸”之前的“红色”:
[^.!?]*?red[^.!?]+?fox[^.!?]*[.!?]
#2
1
For "red" following "fox":
对于“狐狸”之后的“红色”:
\b[^.?!]+red.*?fox[.?!]+
for "fox" following "red":
对于“红色”之后的“狐狸”:
\b[^.?!]+fox.*?red[.?!]+
to capture all other sentences except ones have "red" following "fox":
捕获所有其他句子,除了“狐狸”之后的“红色”:
(?:\b[^.?!]+red.*?fox[.?!]+)(.*?[.?!]+)
as you work with Javascript don't forget to put g
modifier to capture all occurrences:
当您使用Javascript时,不要忘记使用g修饰符来捕获所有出现的内容:
/\b[^.?!]+red.*?fox[.?!]+/g
在线演示
#3
0
Assuming each sentence appears in a separate line you can do:
假设每个句子出现在一个单独的行中,您可以这样做:
I want to match sentences where "red", "fox" both appears in that order
我想匹配“红色”,“狐狸”都按此顺序出现的句子
You can use:
您可以使用:
^.*?red.*?fox.*$
I want to match sentences where "red", "fox" both appears in ANY order
我希望匹配“红色”,“狐狸”都以任何顺序出现的句子
You can use positive lookahead
:
你可以使用积极的前瞻:
^(?=.*?red)(?=.*?fox).*$
#1
1
Assuming that there is no abbreviations with dots in your sentences:
假设句子中没有带点的缩写:
for any order:
对于任何订单:
(?=[^.!?]*fox)(?=[^.!?]*red)[^.!?]+[.!?]
for "red" before "fox":
对于“狐狸”之前的“红色”:
[^.!?]*?red[^.!?]+?fox[^.!?]*[.!?]
#2
1
For "red" following "fox":
对于“狐狸”之后的“红色”:
\b[^.?!]+red.*?fox[.?!]+
for "fox" following "red":
对于“红色”之后的“狐狸”:
\b[^.?!]+fox.*?red[.?!]+
to capture all other sentences except ones have "red" following "fox":
捕获所有其他句子,除了“狐狸”之后的“红色”:
(?:\b[^.?!]+red.*?fox[.?!]+)(.*?[.?!]+)
as you work with Javascript don't forget to put g
modifier to capture all occurrences:
当您使用Javascript时,不要忘记使用g修饰符来捕获所有出现的内容:
/\b[^.?!]+red.*?fox[.?!]+/g
在线演示
#3
0
Assuming each sentence appears in a separate line you can do:
假设每个句子出现在一个单独的行中,您可以这样做:
I want to match sentences where "red", "fox" both appears in that order
我想匹配“红色”,“狐狸”都按此顺序出现的句子
You can use:
您可以使用:
^.*?red.*?fox.*$
I want to match sentences where "red", "fox" both appears in ANY order
我希望匹配“红色”,“狐狸”都以任何顺序出现的句子
You can use positive lookahead
:
你可以使用积极的前瞻:
^(?=.*?red)(?=.*?fox).*$