不太熟悉rubular,想知道如何使用Ruby正则表达式从
中提取“postreview-should-be-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", "\"}"]
仍在尝试各种方式。提前谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以使用string.scan
代替string.split
> "{\"source_url\"=>\"http://testing.com/projects/postreview-should-be-the-cause\"}".scan(/(?<=\/)[^\/"]*(?=[^\/]*$)/)[0]
=> "postreview-should-be-the-cause"
答案 2 :(得分:0)