1 or 0 times repetition quantifier ?

Q

What is the 1 or 0 times repetition quantifier? How to use it?

How to write a regular expression using a 1 or 0 times repetition quantifier to capture both spellings of skilful and skillful?

✍: FYIcenter.com

A

The "1 or 0 times" repetition quantifier allows us to create a special logical OR operation of "match the pattern unit or nothing". The "1 or 0 times" repetition quantifier character is '?' and it needs be placed immediately after the pattern unit. For example:

colou?r      # repeat 'u' 1 or 0 times, same as colo(u|)r
arm(strong)? # repeat 'strong' 1 or 0 times, same as arm(strong|)

The regular expression to capture both skilful and skillful is:

skill?ful

l? - repeat 'u' 1 or 0 times

Click the button to test this regular expression here online:

2013-01-26, 0👍, 0💬