The caret anchor (^) matches differently in Python and Ruby
One thing I’ve come to learn is that there is no “one” regex syntax, and it varies across different languages. Today I learnt another difference!
| Anchor | Python (re) | Ruby |
|---|---|---|
^ | Start of string by default, start of line in MULTILINE mode | Start of line (always) |
\A | Start of string | Start of string |
I discovered this while trying to write a regex in Ruby that would match a word if it appears at the start of the string or the start of a line.
I’m most familiar with Python’s regex syntax, and I got confused. I learnt about the \A anchor while reading the Ruby docs, and then I looked for it in the Python docs. Comparing the two is what made me realise the caret behaves differently across the two languages.
In this particular case, I can write my regex as %r{^hello} and that matches the start of the string or the start of the line, which is simpler than what I expected.