Regular expression to match the whole word

I want to match a regular expression for a whole word.

In the following example, I am trying to match s or season , but I have matches s , e , a , o and n .

 [s|season] 

How to make a regular expression to match a whole word?

+54
regex expression word
Aug 23 '13 at 12:05 on
source share
5 answers

The square brackets are for the character class, and you are actually trying to match any of: s , | , s (again), e , a , s (again), o and n .

Use parentheses instead of grouping:

 (s|season) 

or not an exciting group:

 (?:s|season) 



Note. Non-capture groups inform the engine that it does not need to keep a match, while the other (capture group). For small things, how it works, for "heavy" material, you can first see if you need a match or not. If you do not, it is best to use a non-capturing group to allocate more memory for calculation, rather than storing something you will never need.

+63
Aug 23 '13 at 12:07 on
source share

Use this online example to test your template:

enter image description here

Above is a screenshot taken from this live example: https://regex101.com/r/cU5lC2/1

Matches any whole word on the command line.

I will use the phpsh interactive shell on Ubuntu 12.10 to demonstrate the PCRE regex engine through a method known as preg_match

Run phpsh, put some content in a variable, match the word.

 el@apollo:~/foo$ phpsh php> $content1 = 'badger' php> $content2 = '1234' php> $content3 = '$%^&' php> echo preg_match('(\w+)', $content1); 1 php> echo preg_match('(\w+)', $content2); 1 php> echo preg_match('(\w+)', $content3); 0 

The preg_match method used the PCRE engine in PHP to analyze variables: $content1 , $content2 and $content3 with the pattern (\w)+ .

$ content1 and $ content2 contain at least one word, $ content3 does not.

Matches specific words on the command line without slots

 el@apollo:~/foo$ phpsh php> $gun1 = 'dart gun'; php> $gun2 = 'fart gun'; php> $gun3 = 'darty gun'; php> $gun4 = 'unicorn gun'; php> echo preg_match('(dart|fart)', $gun1); 1 php> echo preg_match('(dart|fart)', $gun2); 1 php> echo preg_match('(dart|fart)', $gun3); 1 php> echo preg_match('(dart|fart)', $gun4); 0 

The variables gun1 and gun2 contain the string dart or fart , which is correct, but gun3 contains darty and still corresponds to this problem. So, in the following example.

Match specific words on the command line with layers:

Word borders can be matched using \b , see Visual analysis of what wordboundary does from jex.im/regulex

Regex Visual Image obtained from http://jex.im/regulex and https://github.com/JexCheng/regulex Example:

 el@apollo:~/foo$ phpsh php> $gun1 = 'dart gun'; php> $gun2 = 'fart gun'; php> $gun3 = 'darty gun'; php> $gun4 = 'unicorn gun'; php> echo preg_match('(\bdart\b|\bfart\b)', $gun1); 1 php> echo preg_match('(\bdart\b|\bfart\b)', $gun2); 1 php> echo preg_match('(\bdart\b|\bfart\b)', $gun3); 0 php> echo preg_match('(\bdart\b|\bfart\b)', $gun4); 0 

\b claims that we have a word boundary, making sure the β€œdart” matches, but the β€œdarty” is not.

+62
Jan 06 '14 at
source share

[ ] defines a character class. That way, every character you set there will correspond. [012] will match 0 or 1 or 2 and [0-2] behaves the same.

What you want is groupings to define or-statement. Use (s|season) for your problem.

Btw. you must follow. Metacharacters in normal regular expression (or within a grouping) differ from the character class. A character class is like a sublanguage. [$A] will only match $ or A , nothing else. Do not run here for a dollar.

+2
Aug 23 '13 at 12:09 on
source share

I am testing examples in js. The simplest solution is to simply add the word u inside inside //:

 var reg = /cat/; reg.test('some cat here');//1 test true // result reg.test('acatb');//2 test true // result 



Now, if you need this particular word with borders, and not inside any other letter signs. We use marker b :

 var reg = /\bcat\b/ reg.test('acatb');//1 test false // result reg.test('have cat here');//2 test true // result 

We also have an exec () method in js that returns a result object. This helps, for example, to obtain information about the place / index of our word.

 var matchResult = /\bcat\b/.exec("good cat good"); console.log(matchResult.index); // 5 

If we need to get all matching words in a line / sentence / text, we can use the g-modifier (global matching):

 "cat good cat good cat".match(/\bcat\b/g).length // 3 

Now the last - I do not need 1 specific word, but some of them. We use | sign, it means choice / or.

 "bad dog bad".match(/\bcat|dog\b/g).length // 1 
+2
Nov 12 '16 at 13:33
source share

For case insensitive search use

 ([sS][eE][aA][sS][oO][nN]) 

To search in lowercase

 ([s][e][a][s][o][n]) 

To search for uppercase

 ([s][e][a][s][o][n]) 
-7
May 20 '16 at 12:33
source share



All Articles