End of Line Character

The $ character is used to match the end of line when performing a regular expression search. For example, given the following text we might want to find all our thresholds except the one that has a comment after it.

 

#define threshold_low    2400

#define threshold_high   4800

#define threshold_mid    3600

#define threshold_enable 1     // Some comment about the enable field

 

To do this we could use the following expression:

 

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

 

This would match the following:

 

#define threshold_low    2400.

#define threshold_high   4800.

#define threshold_mid    3600.

#define threshold_enable 1     // Some comment about the enable field

 

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-9]+$

 

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_enable 1     // Some comment about the enable field

 

The second part,

 

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

 

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_enable 1     // Some comment about the enable field

 

This still doesn't get the correct 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-9]+$

 

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_enable.1     // Some comment about the enable field

 

Now we'll 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-9]+$

 

This yields the following search result:

 

#define threshold_low....2400.

#define threshold_high...4800.

#define threshold_mid....3600.

#define threshold_enable.1.     // Some comment about the enable field

 

However, now we've gone a little too far because we've selected threshold_enable that wasn't supposed to be part of our selection. To prevent selecting this we can use the $ character to match the end of line sequence, leading to the last part of our regular expression search string:

 

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

 

This yields the following search result:

 

#define threshold_low....2400.

#define threshold_high...4800.

#define threshold_mid....3600.

#define threshold_enable.1.     // Some comment about the enable field

 

Next we'll look at the regular expression matching using the start of a word.

 

Back: Start of Line

Forward: Start of Word