Rob's Tips and TricksSunday, August 9, 2009String#extract is an extension to the core String class that simplifies extracting values from a string using regular expressions.
The codeFirst, let me show you the method:
So, what does it do?I always find using String#match and its Regex equivalents to be tedious and hard to follow. Here's an example of trying to pull the various phone fields out of a user-entered phone number:
Instead, check out using the extract method:
Basically, #extract allows you to use a regex to pull a value out of a string instance, cleanly. Here are a few more examples:
So there you have it. Extract a single value with a plain regular expression, or one or more values using regexen with capture groups in them. And the syntax is clean and elegant. Hope you like it!
Posted
by Rob
at 9:09 PM
1 comment
Sunday, August 2, 2009Thanks for stopping by. My name is Rob Morris, and I'm a web developer/software architect/code dude located in North Carolina. I've set up this blog as a home for tips and techniques that I run across or develop in the line of duty.
I primarily work in Ruby/Rails these days, with a heavy helping of jQuery and PHP on the side, so that's what you'll mostly find in these posts. I hope you find the information useful, and please feel free to join the discussion. Cheers! -Rob
Posted
by Rob
at 9:09 PM
Add a comment
|
AboutWeb home of Rob Morris, software guru and all-around great guy. A place to post and discuss code and the software development lifestyle. ArchiveJuly 2010 (1)September 2009 (1)August 2009 (2) |