Start of Line Character

In addition to inverting character classes, the ^ character is also used in regular expression searches to match the beginning of a new line.

 

#define threshold_low   2400

#define threshold_high  4800

#define threshold_mid   3600

#define threshold_other 0x4000

 

We can match the entire definition of our defines using the following regular expression example:

 

^#define threshold[A-Za-z_]+[ \t]+[0-9x]+

 

This expression will match the following:

 

#define threshold_low   2400.

#define threshold_high  4800.

#define threshold_mid   3600.

#define threshold_other 0x4000.

 

Understanding the Expression

Let's break down the expression to understand how it works. The first part is highlighted below:

 

^#define threshold[A-Za-z_]+[ \t]+[0-9x]+

 

This sequence means match any sequences of #define threshold_ that starts at the beginning of a line as indicated by the ^ character. Thus we would get the following four matches:

 

#define threshold_low   2400

#define threshold_high  4800

#define threshold_mid   3600

#define threshold_other 0x4000

 

The second part,

 

^#define threshold[A-Za-z_]+[ \t]+[0-9x]+

 

extends our match to find one or more characters in the set A-Z, a-z, or the _ character. Thus our new match would look like:

 

#define threshold_low   2400

#define threshold_high  4800

#define threshold_mid   3600

#define threshold_other 0x4000

 

This still doesn't get our entire selection so we extend it to match any whitespace characters that separate our threshold name and its defined value. This leads us to the following expression

 

^#define threshold[A-Za-z_]+[ \t]+[0-9x]+

 

In this case we've used the [ \t]+ sequence to match a series of one or more whitespace characters (space or tab). Our new selection will look like:

 

#define threshold_low...2400

#define threshold_high..4800

#define threshold_mid...3600

#define threshold_other.0x4000

 

The last part of our regular expression is used to match the numbers that define our thresholds. For this we use the character class [0-9x]+ to match a sequence of one or more digits or the character x.

 

^#define threshold[A-Za-z_]+[ \t]+[0-9x]+

 

This yields the following search result:

 

#define threshold_low...2400.

#define threshold_high..4800.

#define threshold_mid...3600.

#define threshold_other.0x4000.

 

Next we'll look at an example of the end of line regular expression character.

 

Back: Asterisk Character

Forward: End of Line Character

.