Searching with the Plus Character

The plus character is used to match a sequence of one or more characters in a character sequence.

 

Going back to our constants example:

 

#define threshold_low  2400

#define threshold_high 4800

#define threshold_mid  3600

 

Our previous matching string was:

 

threshold_[A-Za-z]

 

This only matched threshold_l, threshold_h, and threshold_m because the character sequence [A-Za-z] only matches a single character. This time we want to improve our match such that we match our entire string instead of just the first character. To do this we can use the plus sign after our character class to match all characters after the underscore up to the first whitespace character. Our new expression becomes:

 

threshold_[A-Za-z]+

 

This will now match the following strings:

 

#define threshold_low  2400

#define threshold_high 4800

#define threshold_mid  3600

 

It will stop at the first character that is not in the given set. However, if we had an expression:

 

#define threshold_

 

it would not match and we would get the following:

 

#define threshold_low  2400

#define threshold_high 4800

#define threshold_mid  3600

#define threshold_

 

This is because the + character matches 1 or more occurrences of the character specified in our group. In the last example, threshold_ has 0 or more characters in our set [A-Za-z] so it does not.

 

We could have also used the asterisk regex character to perform the same search. Let's look at an example of this character now.

  

 

Back: Inverse Character Classes

Forward: Asterisk Character