Lesson 22d: Regular Expression: Alternatives and Grouping

Alternatives and Grouping

A set of alternative patterns can be specified with the | symbol:

1
/wolf|sheep/;   # matches "wolf" or "sheep"

Use parenthesis with alternative patterns:

1
2
/big bad (wolf|sheep)/;
# matches "big bad wolf" or "big bad sheep"

Exercises

  1. Create a regular expression that will match a string with this pattern ATG followed by a C or a T. Test your regular expression with these two strings:
    • GCTGATGCGTTA
    • GCTATGGCT

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *