Live data from Hacker News

What are your Ruby Regex Idioms?

blog.samstokes.co.uk

1–10 of 24 posts

Re: What are your Ruby Regex Idioms?

#3
post #2

One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")

Nice. I am intrigued by the leading underscore, are you just using that as a throwaway variable? You could get the username and domain on their own with:

  username, domain = [*/([^@]+)@(.+$)/.match("foo@example.com")][1..-1]
And it is super readable.

Re: What are your Ruby Regex Idioms?

#5
post #2

One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")

If you're capturing something you don't want, you can always prefix your match with '?:' to have a non-capturing group.

FWIW, I personally prefer the #match method. Some of the examples in this article just make me want to hurt people. Especially usage of $1, $2, etc.

Re: What are your Ruby Regex Idioms?

#7
post #5
post #2

One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")

If you're capturing something you don't want, you can always prefix your match with '?:' to have a non-capturing group. FWIW, I personally prefer the #match method. Some of the examples in this article just make me want to hurt people. Especially usage of $1, $2, etc.

In that case, /([^@]+)@(.+$)/.match("foo@example.com")[0] returns the whole string matched by the expression, and [1] and [2] return the first and second groups. That's why the throwaway is there. To get rid of the throwaway, this is a possibility:

  username, domain = */([^@]+)@(.+$)/.match("foo@example.com").captures
Of course, your string better match, otherwise you'll get a NoMethodError.

Re: What are your Ruby Regex Idioms?

#8
post #4

Woah.. I want that for Python..

I sure don’t. Putting a regular expression in a slice for a string runs totally counter to all existing python idioms, and would utterly confuse anyone trying to read the code. There are definitely other ways of accomplishing this with reasonably compact syntax that are also intuitive and clear. (But I'm unconvinced it needs special syntax at all. It saves a couple lines here and there, maybe, but at the cost of making the language more complex.)

  caller[0][/`([^']*)'/, 1]
isn't really so much shorter than:

  re.search("`([^']*)'", caller[0]).group(1)
and the latter is quite a bit more explicit.

Re: What are your Ruby Regex Idioms?

#9
post #5
post #2

One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")

If you're capturing something you don't want, you can always prefix your match with '?:' to have a non-capturing group. FWIW, I personally prefer the #match method. Some of the examples in this article just make me want to hurt people. Especially usage of $1, $2, etc.

$1 and $2 are probably still there do to Ruby's Perl heritage. I don't particularly mind their use, but that's probably due to my long history with Perl.
Post reply on HN