Ruby Regex vs Python Regex

前端 未结 5 454
一个人的身影
一个人的身影 2021-01-12 05:10

Are there any real differences between Ruby regex and Python regex?

I\'ve been unable to find any differences in the two, but may have missed something.

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 05:58

    Ruby's Regexp#match method is equivalent to Python's re.search(), not re.match(). re.search() and Regexp#match look for the first match anywhere in a string. re.match() looks for a match only at the beginning of a string.

    To perform the equivalent of re.match(), a Ruby regular expression will need to start with a ^, indicating matching the beginning of the string.

    To perform the equivalent of Regexp#match, a Python regular expression will need to start with .*, indicating matching zero or more characters.

提交回复
热议问题