Antony Sastre
Antony Sastre

Reputation: 617

How to get regex to ignore URL strings?

I have the following Regexp to create a hash of values by separating a string at a semicolon:

Hash["photo:chase jarvis".scan(/(.*)\:(.*)/)] 
// {'photo' => 'chase jarvis'}

But I also want to be able to have URL's in the string and recognize it so it maintains the URL part in the value side of the hash i.e:

Hash["photo:http://www.chasejarvis.com".scan(/(.*)\:(.*)/)] 
// Results in {'photo:http' => '//www.chasejarvis.com'}

I want, of course:

Hash["photo:chase jarvis".scan(/ ... /)] 
// {'photo' => 'http://www.chasejarvis.com'}

Upvotes: 0

Views: 332

Answers (2)

David Webb
David Webb

Reputation: 193716

If you only want to match up to first colon you could change (.*)\:(.*) to ([^:]*)\:(.*).

Alternatively, you could make it a non-greedy match, but I prefer saying "not colon".

Upvotes: 2

TK.
TK.

Reputation: 28153

How do figure out a person's family name and first name?

Changing chasejarvis to chase and jarvis might not be possible unless you have a solution for that.

Do you already know everyone's name in your project? Nobody is having the initial of a middle name like charvisdjarvis (assuming the name is "Charvis D. Jarvis".)?

Upvotes: 0

Related Questions