[General boards] [Fall 2018 courses] [Summer 2018 courses] [Winter 2018 courses] [Older or newer terms]

Regarding anchor for regex


when writing regex, don’t we need to write starting and ending anchor for each one so that the answer is for a string but not just a pattern?

ex: write a regex that matches only non-empty binary strings of 1s and 0s(eg, 1, 0, 10101010 and so on)
so will the answer be [01]+ or ^[01]+$?


The word “only” suggest that anchors are necessary. Otherwise, the regex might match larger strings containing that substring.

On the other hand, if the question asked “Write a regex that matches only non-empty substrings of…” then anchors are not necessary.