[英]ruby regex: How to extract string from url
對rubular不太熟悉,想知道如何使用Ruby regex從中提取“ postreview-should-the-cause”原因
"{\"source_url\"=>\"http://testing.com/projects/postreview-should-be-the-cause\"}"
我得到的最好的是
check_user = url.split(/\b(\w+)\b/)
=> ["{\"", "source_url", "\"=>\"", "http", "://", "localhost", ":", "3000", "/", "projects", "/", "postreview", "-", "should", "-", "be", "-", "the", "-", "cause", "\"}"]
仍在嘗試各種方法。 提前致謝。
您可以使用string.scan
而不是string.split
> "{\"source_url\"=>\"http://testing.com/projects/postreview-should-be-the-cause\"}".scan(/(?<=\/)[^\/"]*(?=[^\/]*$)/)[0]
=> "postreview-should-be-the-cause"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.