Skip to main content

The caret anchor (^) matches differently in Python and Ruby

  • Tagged with regex
  • Posted

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!

AnchorPython (re)Ruby
^Start of string by default, start of line in MULTILINE modeStart of line (always)
\AStart of stringStart 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.