Summary
This is just the tiniest tip of the iceberg of what regular expressions can do. In other words, even though you’re completely overwhelmed by them now, believe me, you ain’t seen nothing yet.
You should now be familiar with the following techniques:
^
matches the beginning of a string.$
matches the end of a string.\b
matches a word boundary.\d
matches any numeric digit.\D
matches any non-numeric character.x?
matches an optionalx
character (in other words, it matches an x zero or one times).x*
matchesx
zero or more times.x+
matchesx
one or more times.x{n,m}
matches anx
character at leastn
times, but not more than m times.(a|b|c)
matches exactly one ofa
,b
orc
.(x)
in general is a remembered group. You can get the value of what matched by using thegroups()
method of the object returned byre.search
.
Regular expressions are extremely powerful, but they are not the correct solution for every problem. You should learn enough about them to know when they are appropriate, when they will solve your problems, and when they will cause more problems than they solve.
Get hands-on with 1400+ tech skills courses.