Quantifiers

Quantifers instruct the regex parser to look for zero or more repetitions (you can quantify how many, hence the name) of a particular pattern of text.

example

Let’s say you want to match text within html tags. We need to tell the parser to: match the HTML tags, match whatever is between them and match the end the HTML end tag. Our regex would be:  <b>.+?</b> which breaks down as

<b> match the bold tag open
.+? match one or more chars lazily
</b> match the bold tag close

The .+? part of the regex deserves further breakdown.

. match any char
+ match the char one or more times
? match lazily

Matching ‘Lazily’ means that the parser should attempt as few matches as possible whilst still matching the regex as a whole. This is in contrast to matching Greedily (achieved by simply missing out the ‘?’, where as many matches as possible will be made.

To illustrate the difference, let’s take a fragment from a HTML document as below:

<b>I am some bolded text</b> I am some unbolded text<b>I am more bolded text</b>

Now if we apply our lazy matching regex we’ll get the following two matches. <b>I am some bolded text</b> I am some unbolded text <b>I am more bolded text</b>

But if we applied a Greedy match, we’d get the following single match: <b>I am some bolded text</b> I am some unbolded text <b>I am more bolded text</b>

More on Quantifers

Quantifers can also:

Match between zero and unlimited numbers of chars, instead of between 1 and unlimited as in our example.You use ‘*’ instead of a ‘+’ to do this.

Match a bounded number of repetitions. You forgo the ‘+’ or ‘*’ and use the {min,max} format for this for example: a{1,10} if you want to match between 1 and 10 chars.  Leaving the min or max blank specifies no minimum or maximum.

Can be used anywhere in the expression

Textpression

Here are some of the ways you could view the completed regex in Textpression

You can paste plain text regex straight into Textpression and get an immediate visualisation as above.

Or you can create regex yourself using the simple drag and drop editor.

If you’d like to see how easy Quantifiers are in Textpression check out the video.