0
A
回答
2
剛剛得到從0到http://www.google.com
長度的子字符串,就完成了。
0
試試這個:
/\Ahttp:\/\/www\.google\.com(.*)?\Z/
ruby-1.9.2-p0 > "http://www.google.com" =~ /\Ahttp:\/\/www\.google\.com(.*)?\Z/
=> 0
ruby-1.9.2-p0 > "http://www.google.com/foobar" =~ /\Ahttp:\/\/www\.google\.com(.*)?\Z/
=> 0
ruby-1.9.2-p0 > $1
=> "/foobar"
0
Rails有一個便捷的start_with?方法。如果它只是一個靜態字符串,則不需要正則表達式。
url.start_with?("http://www.google.com")
1
而不是使用正則表達式,你可能要考慮使用隨紅寶石URI library。它試圖分解和構建URL,經過充分測試,並且比嘗試重新發明相同功能的錯誤更少。
require 'uri'
url = URI.parse('http://www.google.com/path/to/page.html?a=1&b=2')
url.scheme # => "http"
url.host # => "www.google.com"
url.path # => "/path/to/page.html"
url.query # => "a=1&b=2"
如果這還不夠好,Addressable::URI寶石更顯幹練。
相關問題
- 1. 匹配匹配字符串的正則表達式的子串
- 2. 正則表達式正則表達式匹配字符串
- 3. 正則表達式匹配字符串
- 4. 正則表達式匹配字符串
- 5. 正則表達式匹配字符串
- 6. 正則表達式匹配字符串
- 7. 正則表達式匹配字符串
- 8. 正則表達式匹配字符串
- 9. 正則表達式匹配字符串
- 10. 正則表達式匹配字符串
- 11. 重複字符匹配正則表達式匹配字符串
- 12. Postgres正則表達式子字符串或正則表達式匹配
- 13. 正則表達式來匹配一個url內的字符串
- 14. 從url匹配字符串的正則表達式
- 15. 正則表達式匹配散列URL字符串
- 16. 正則表達式匹配url並忽略查詢字符串
- 17. 正則表達式:不匹配字符串的匹配順序
- 18. 子字符串或完全匹配的正則表達式
- 19. 匹配正則表達式中的可選子字符串
- 20. 拆分最長的正則表達式匹配子字符串
- 21. .NET正則表達式匹配匹配空字符串
- 22. 正則表達式匹配字符串不包含其他子
- 23. Javascript正則表達式 - 匹配2個子字符串
- 24. 正則表達式匹配特定字符串的字符>
- 25. 正則表達式匹配字符串中的連字符
- 26. 正則表達式匹配字符串中的EOL字符
- 27. 正則表達式匹配除字符
- 28. 正則表達式匹配高達「/」的任何字符串Javascript
- 29. 正則表達式僅匹配字符串,而不是子字符串
- 30. 正則表達式幫助,匹配長字符串,選擇子字符串