Ruby Regex
I have a mission. I have been programming for quite a few years, but have always passed at really digging deep in to regex. No longer.
I am going to use this as a placeholder for some of my newfound knowledge. First, I use rubular for testing my expressions. Second, a great resource has been this article by bluebox
So without further ado, here we go-
Match a single character- /(.)/
If we used "testing 123" as our match we would have 11 matches. "testing 123".scan(/(.)/)
Match a group of (any) characters- /(.+)/
This will keep matching until we either come to the end of the line or add another match in.
Positive lookahead /test(?=ing)/
will match testing
, but not testers
or test
. The match will return just the word test.
Positive lookbehind /(?<=wel)come/
will match welcome
but not become
The lookahead and lookbehind assertions also have a negative matcher, which can be invoked by replacing the = with the ! character, like so /(?<!wel)come/
. That will match become
but not welcome
.
That's all for now, but I will keep on searching, and keep on coding.
Written by Brandon Hansen
Related protips
1 Response
Awesome, thanks.