Hommer Smith
Hommer Smith

Reputation: 27852

Regex to get two values in string

I have the following string:

<http://test.host/users?param1=1&param=1>; rel=\"rel_value\"

And I would like to get the URL and the rel value. That is:

http://test.host/users?param1=1&param=1

and

rel_value

I know how to get the URL:

string[/<.*?>/]

But failing to see how to get the rel. Any ideas on a regex that I could get both?

Upvotes: 0

Views: 148

Answers (3)

Cary Swoveland
Cary Swoveland

Reputation: 110675

There should be at least one non-regex solution:

str.tr('<>\\\"','').split(';\s+rel=')
  #=> ["http://test.host/users?param1=1&param=1; rel=rel_value"] 

Upvotes: 0

David Dersigny
David Dersigny

Reputation: 261

As you asked for a regex that does both:

<(.*)>.*rel=\\"(.*)\\"

The first capturing group contains the URL, and the second one the rel value. But you could just do one regex for each. For the URL:

<(.*)>

And for the rel value:

rel=\\"(.*)\\"

Upvotes: 1

Linuxios
Linuxios

Reputation: 35783

If the string is guaranteed to have that format:

/<(.+)>; rel=\\\"(.+)\\\"/

To be used like so:

m = s.match(/<(.+)>; rel=\\\"(.+)\\\"/)
m[0] #=> http://test.host/users?param1=1&param=1
m[1] #=> rel_value

Additionally, you could just use two regexes to search for each thing in the string:

s[/(?<=<).+(?=>)/] #=> http://test.host/users?param1=1&param=1
s[/(?<=rel=\\\").+(?=\\\")/] #=> rel_value

(These use lookahead and lookbehind to not capture anything besides the values).

Upvotes: 4

Related Questions