gFontaniva
gFontaniva

Reputation: 903

How get content between special chars in Ruby on rails?

I'm searching for a method in what give content between special chars. EX:

"This is my string, and < this is the content >"

expect result between < >, "this is the content"

Upvotes: 2

Views: 63

Answers (3)

jonfaulkenberry
jonfaulkenberry

Reputation: 96

You can use the regular expression /<(.*?)>/

str = "This is my string, and <test one> < test two >"
str.scan(/<(.*?)>/)
=> [["test one"], [" test two "]]

Upvotes: 3

MattyB
MattyB

Reputation: 952

You want a non-greedy capture. This is the regular expression:

<(.*?)>

See it here: http://rubular.com/r/7KN1HLipyW

ie

"This is my string, and < this is the content > and <another one>".scan(/<(.*?)>/)

Upvotes: 0

Omid Kamangar
Omid Kamangar

Reputation: 5778

Try this regexp:

"This is my string, and < this is the content > <and more content>. And the tail".scan(/<.*>/)

It gives you an array of occurrences:

=> ["< this is the content > <and more content>"]

Upvotes: 1

Related Questions