Nothing To Lose

If you don’t have it, how can you lose it!
Subscribe

Regular Expression — Use of \B (Not Word Edges) Part 2

March 18, 2009 By: Dexter Category: Regular Expressions, Tutorial

Using \B: Finding pattern at the NOT at LEFT edge of words.

Continues from Previous Post: Regular Expression — Use of \b (Word Edges) Part 1

Enter the following command (note the \B in the start of the pattern)


$grep ‘\Bcat’ wordedge.txt
cat command is used to concatenate two or more files.
Be unique do not be a copycat.


Notice the first line of the output. the cat in ‘concatenate’ is selected as it is not at the (left) edge of the word.

The second line has cat selected in ‘copycat’ as it is not at the left edge of the word.

So if you are using ‘\B’ in the beginning of the pattern then it will look for that pattern after the first character of words.

Using \B: Finding pattern at the NOT at RIGHT edge of words.

Enter the following command (note the \B in the end of the pattern)


$grep ‘cat\B’ wordedge.txt
cat command is used to concatenate two or more files.
old weapon used to hit birds is catapult.


Notice the first line of the output. the cat in ‘concatenate’ is selected as it is not at the (right) edge of the word.
The second line has cat selected in ‘catapult’ as it is not at the right edge of the word.
So if you are using ‘\B’ in the end of the pattern then it will look for that pattern before the last character of words.

Using \Bpattern\B

So what happens when we put ‘\B’ on both sides of out pattern.
Enter the following command (note the \B at both ends of the pattern)


$grep ‘\Bcat\B’ wordedge.txt
cat command is used to concatenate two or more files.


So when ‘\B’ is used on both the sides, then the whole pattern is searched within a word.

It can be interpreted as that ‘cat’ should NOT be the beginning of the word and as well as NOT at the end of the word.. which mean it has to be inside some other word that word.

NEXT: Using \B and \b together

2 Trackbacks/Pingbacks

  1. Find (and replace) my favo(u)rite colo(u)r and much, much more | Hobby Cash: Make Cash Blogging About the Things You Love 26 03 09
  2. concatenate files - StartTags.com 02 03 10

Leave a Reply