Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match exact phrase, nothing before or after the phrase

Tags:

regex

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. 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.

like image 840
phobiac Avatar asked Jul 19 '12 01:07

phobiac


People also ask

How do you match exact strings?

We can match an exact string with JavaScript by using the JavaScript string's match method with a regex pattern that has the delimiters for the start and end of the string with the exact word in between those.

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What does \+ mean in regex?

Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol. Example: "^a" matches "a" at the start of the string. Example: "[^0-9]" matches any non digit.

How do you use regex with no characters after it?

Allows the regex to match the word if it appears at the end of a line, with no characters after it. | indicates an “or,” so the regex matches any one of the words in the list. s matches a space character. Use this character to separate words in a phrase.

Do you want to match an exact word or phrase?

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.

What is the difference between ^ and $ in regex?

^ matches the start of a new line. Allows the regex to match the number if it appears at the beginning of a line, with no characters before it. $ matches the end of a line. Allows the regex to match the number if it appears at the end of a line, with no characters after it.

What is a regular expression?

A regular expression that matches a slash at the end of a string. A regular expression that matches the first word after a specific word in a sentence. A regular expression that determines if a given string has all unique (none repeating) characters. A regular expression that matches multiple Email addresses separated by semicolons in a string.


2 Answers

To match the exact line "Mountain" use this: /^Mountain$/

You haven't mentioned what language you're working in, so the exact form of the pattern might have to change.

like image 108
Ned Batchelder Avatar answered Sep 19 '22 14:09

Ned Batchelder


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$ 
like image 21
Carl Avatar answered Sep 18 '22 14:09

Carl