101-400 · Question #411
When using regular expressions, which of the following characters match the beginning of a line?
The correct answer is A. ^. See the full explanation below for the reasoning.
Question
Options
- A^
- B?
- C
- D
- E$
How the community answered
(42 responses)- A79% (33)
- B2% (1)
- C12% (5)
- D5% (2)
- E2% (1)
Community Discussion
5A is correct, full stop. The caret anchors a pattern to the start of a line, so something like ^error only matches if "error" appears at the very beginning, not buried in the middle. The other four are quantifiers or a different anchor: question mark means zero or one, asterisk means zero or more, plus means one or more, and dollar is the end-of-line anchor, the mirror image of caret. Confusing caret with the others is a sign you memorized a chart instead of thinking about what anchoring even means, so burn this into your head now before it bites you on a real grep command at 2am.
Does your regex engine treat ^ differently in multiline mode versus single-line, or are you just memorizing A for the exam?
Real distinction worth knowing cold, and I would add that confusing ^ with \A trips people up just as often because \A always anchors to the absolute start of the string no matter what flags you set, so if the exam gives you a multiline pattern and asks what \A matches, the answer is never "each line."
C for sure, the asterisk anchors you right to line start.
Wesley, the asterisk means zero or more of the preceding character, it does not anchor anything, that is what the caret does, so A is the correct pick here.