Ruby中的string.match(regex)和regex.match(string)有什麼區別?在語言中使用這兩種構造的理由是什麼?string.match(regex)vs regex.match(string)
7
A
回答
1
我thnk的是,直覺,match
,或相關方法=~
,表達了某種平等的,所反映的事實=~
包括平等=
和等價~
關係(不是紅寶石,但在數學)。但它並不完全是等價關係,而且在平等的三個公理(反射性,交換性,傳遞性)中,尤其是交換性似乎在這種關係中得到了保持;對於程序員來說,期望string.match(regex)
或string =~ regex
意味着與regex.match(string)
或regex =~ string
相同的意思是很自然的。我自己,會記住問題,如果任何一個被定義而不是另一個。事實上,有些人感到奇怪的是,也提醒我們某種平等的方法===
不可交換,並且已經提出了question。
4
除了掛斷不同的對象(有時候可以更方便地調用一個而不是另一個),它們是相同的。理由是它們都是有用的,有時比另一個更方便。
相關問題
- 1. CMake STRING REGEX REPLACE
- 2. Regex.Match和noncapturing groups
- 3. Python/Regex - Match。#,#。 in String
- 4. TypeScript - [string] vs string []
- 5. TextBox.Text + =「string」; vs TextBox.AppendText(「string」);
- 6. RegEx Whitespace Vs. Eclipse的
- 7. nginx:location tilde regex vs path
- 8. Regex.Match整個單詞
- 9. Lua string.match問題?
- 10. str.equals(「String」)vs「String」.equals(str)
- 11. joda-time:new DateTime(String)vs DateTime.parse(String)
- 12. String.Format vs「string」+「string」或StringBuilder?
- 13. string ss =「」; vs string ss = string.empty; vs string = null;在c#
- 14. performance stringbuf vs string
- 15. JAVA String vs StringBuffer
- 16. Lua string.match()問題
- 17. RegularValidator與Regex.Match在CustomValidator中
- 18. 我可以使用什麼作爲Regex.Match(String,Int32)的靜態等價物?
- 19. Microsoft office Access`LIKE` VS`RegEx`
- 20. c#Regex.Match問題
- 21. C#Regex.Match to decimal
- 22. Regex.Match問題c#
- 23. Regex.Match大寫
- 24. Regex.Match引發FormatException
- 25. 如何選擇與Regex.Match
- 26. XML false vs(string)false
- 27. string&vs. wstring&in runtime_error
- 28. Sql,Object vs String/Array
- 29. writeUTF(String s)將VS
- 30. String.format()vs string concatenation performance
我不太喜歡這種說法(可能沒有比「他們在那裏,所以活在其中」更好)。它們都很有用,因爲它們都具有*相同的用途。我可以說,一個的存在使另一個無用。 – 2011-04-09 20:45:37
除了它們沒有相同的用途。他們只有非常相似的用途。無論如何,我不相信他們需要理由。如果你真的想要一個需要證明的Ruby語言功能(並且缺少它),請在'for ... in ...'循環中進行操作。 – 2011-04-09 20:51:53
所以他們不一樣?你能改善你的答案以反映這一點嗎? – 2011-04-09 21:03:16