简体   繁体   English

正则表达式匹配确切的短语,短语之前或之后均不匹配

[英]Regex to match exact phrase, nothing before or after the phrase

I am not sure if this is exactly possible. 我不确定这是否完全可能。 Consider this example set of data: 考虑以下示例数据集:

Mountain
Big mountain
Mountain of stone
A mountain on a hill

I want to match Mountain. 我要匹配山。 Nothing else. 没有其他的。 No other parts of that set. 该集合中没有其他部分。 Just the exact line, Mountain. 确切的路线,山。 Everything I've tried either matches for all instances of Mountain or for none of them. 我尝试过的所有内容都可以匹配Mountain的所有实例,也可以不匹配。 Plenty of people want to match an exact word or phrase, but I seem to be the only one who wants to match only one exact word or phrase. 很多人都想匹配一个精确的单词或短语,但我似乎是唯一想匹配一个精确的单词或短语的人。

If this CAN be expanded to a phrase that would be perfect. 如果可以将其扩展为一个完美的短语。 Assume: 假设:

Go for a hike
Go for a hike, on a mountain.
I want to go for a hike.

Where I want to match only "Go for a hike" but no phrase that contains it. 我只想匹配“去远足”,而没有包含它的短语。

To match the exact line "Mountain" use this: /^Mountain$/ 要匹配确切的行“ Mountain”,请使用: /^Mountain$/

You haven't mentioned what language you're working in, so the exact form of the pattern might have to change. 您没有提到您使用的语言,因此模式的确切形式可能需要更改。

If you have a string that contains multiple lines, the following two regular expressions may help: 如果您的字符串包含多行,则以下两个正则表达式可能会有所帮助:

\r\nWORD\r\n|^WORD\r\n|\r\WORD$|^WORD$

\r\nA\sPHRASE\r\n|^A\sPHRASE\r\n|\r\A\sPHRASE$|^A\sPHRASE$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM