Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page


By default, a regular expression will try to find the first match for the pattern in a string. Match /iss/ against the string ``Mississippi,'' and it will find the substring ``iss'' starting at position one. But what if you want to force a pattern to match only at the start or end of a string?

The patterns ^ and $ match the beginning and end of a line, respectively. These are often used to anchor a pattern match: for example, /^option/ matches the word ``option'' only if it appears at the start of a line. The sequence \A matches the beginning of a string, and \z and \Z match the end of a string. (Actually, \Z matches the end of a string unless the string ends with a ``\n'', it which case it matches just before the ``\n''.)

showRE("this is\nthe time", /^the/) this is\n<<the>> time
showRE("this is\nthe time", /is$/) this <<is>>\nthe time
showRE("this is\nthe time", /\Athis/) <<this>> is\nthe time
showRE("this is\nthe time", /\Athe/) no match

Similarly, the patterns \b and \B match word boundaries and nonword boundaries, respectively. Word characters are letters, numbers, and underscore.

showRE("this is\nthe time", /\bis/) this <<is>>\nthe time
showRE("this is\nthe time", /\Bis/) th<<is>> is\nthe time

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire