0

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?

2
  • Does the string actually contain those backslashes? Commented Oct 8, 2015 at 18:39
  • yes it does contain those backslashes Commented Oct 8, 2015 at 18:41

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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=\\"(.*)\\"

Comments

0

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"] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.