What are your Ruby Regex Idioms?
blog.samstokes.co.uk
What are your Ruby Regex Idioms?
1–10 of 24 posts
Re: What are your Ruby Regex Idioms?
#2 _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")Re: What are your Ruby Regex Idioms?
#3One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")
username, domain = [*/([^@]+)@(.+$)/.match("foo@example.com")][1..-1]
And it is super readable.Re: What are your Ruby Regex Idioms?
#4Re: What are your Ruby Regex Idioms?
#5One of my favorite patterns: _, username, domain = */([^@]+)@(.+$)/.match("foo@example.com")
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?
#6Re: What are your Ruby Regex Idioms?
#7One 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.
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?
#8Woah.. I want that for Python..
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?
#9One 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.