0

对 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", "\"}"]

还在尝试各种方法。提前致谢。

4

3 回答 3

1

要从给定字符串中提取该子字符串,您可以使用以下内容进行匹配而不是拆分。

result = url.match(/\w+(?:-\w+)+/)

工作演示

于 2015-01-13T04:47:18.067 回答
0

你可以使用string.scan而不是string.split

> "{\"source_url\"=>\"http://testing.com/projects/postreview-should-be-the-cause\"}".scan(/(?<=\/)[^\/"]*(?=[^\/]*$)/)[0]
=> "postreview-should-be-the-cause"
于 2015-01-13T04:46:19.453 回答
0
\/(?!.*\/)

按此拆分。并获得第二个组件。参见演示。

https://regex101.com/r/sH8aR8/48

于 2015-01-13T04:47:17.813 回答