# Regular expressions for beginners

**URL:** <https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818>\
**Category:** Ruby\
**Created:** [January 7, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818 "2006-01-07T15:53:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 7, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/1 "2006-01-07T15:53:45Z")

</div>

There’s been a number of questions about regular expressions in recent  
days.

The Ruby U.'s Guide has a small script to highlight regular  
expression patterns. I found it quite helpful for visualizing regex’s,  
especially while learning them.

[http://www.ruby-doc.org/docs/UsersGuide/rg/](http://www.ruby-doc.org/docs/UsersGuide/rg/)

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 7, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/2 "2006-01-07T16:08:48Z")

</div>

Jim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://www.ruby-doc.org/docs/UsersGuide/rg/](http://www.ruby-doc.org/docs/UsersGuide/rg/)  
> i don’ get colors even with the ansi termianl over MacOS X…

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 7, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/3 "2006-01-07T20:34:03Z")

</div>

Works here[1]. What is your $TERM set at?

[1] After changing:  
str.gsub! re, “#{st}\&#{en}” to  
str.gsub!(/#{re}/, “#{st}\&#{en}”) # gsub takes a string /or/  
regular-expression

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 7, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/4 "2006-01-07T21:40:21Z")

</div>

Daniel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> str.gsub!(/#{re}/, “#{st}\&#{en}”)

thanks, right now i get reverse video for match.

i’m sure my Terminal works with ansi colors because recently i’ve tried  
xml-smart “xpath\_visual.rb” with term-ansicolor-0.0.4.

see for example [http://cjoint.com/data/bhvDrQHhrH.htm](http://cjoint.com/data/bhvDrQHhrH.htm)

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 7, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/5 "2006-01-07T23:08:11Z")

</div>

Jim wrote:

> The Ruby U.'s Guide has a small script to highlight regular  
> expression patterns. I found it quite helpful for visualizing regex’s,  
> especially while learning them.
> 
> [http://www.ruby-doc.org/docs/UsersGuide/rg/](http://www.ruby-doc.org/docs/UsersGuide/rg/)

There’s also a small tool (GUI) bundled with Ruby Development Tools for  
Eclipse that does just that. Quite useful.

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 9, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/6 "2006-01-09T13:38:42Z")

</div>

On Mon, Jan 09, 2006 at 06:08:11PM +0900, Gene T. wrote:

> >

The clearest explanation I recall seeing was . . . I think it was  
chapter seven in Learning Perl (I have 2nd Ed.: later editions might  
have it in a different chapter for all I know). Yes, I know, it’s Perl  
and not Ruby – but that doesn’t mean it’s not useful knowledge.

–  
Chad P. [CCD CopyWrite | [http://ccd.apotheon.org](http://ccd.apotheon.org) ]

print substr(“Just another Perl hacker”, 0, -2);

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 9, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/7 "2006-01-09T14:05:50Z")

</div>

There is an excellent book from O’Reilly -

> **[Regular Expression Pocket Reference, 2nd Edition](https://www.oreilly.com/library/view/regular-expression-pocket/9780596514273/)**
>
> This handy little book offers programmers a complete overview of the syntax and semantics of regular expressions that are at the heart of every text-processing application. Ideal as a quick … - Selection from Regular Expression Pocket Reference, 2nd...

I’ve read it and it is by far and large the best regex tutorial  
available.

“Mastering Regular Expressions, Second Edition”

By Jeffrey E. F. Friedl  
Second Edition July 2002  
ISBN: 0-596-00289-0

* * *

Full Description

Regular expressions are an extremely powerful tool for manipulating text  
and  
data. They have spread like wildfire in recent years, now offered as  
standard features in Perl, Java, [VB.NET](http://VB.NET) and C# (and any language using  
the  
.NET Framework), PHP, Python, Ruby, Tcl, MySQL, awk, Emacs, and many  
other  
popular tools and languages.

If you don’t use regular expressions yet, you will discover in this book  
a  
whole new world of mastery over your data. If you already use them,  
you’ll  
appreciate this book’s unprecedented detail and breadth of coverage. If  
you  
think you know all you need to know about regular expressions, this book  
is  
a stunning eye-opener.

In spite their wide availability, flexibility, and unparalleled power,  
regular expressions are frequently underutilized. Regular expressions  
allow  
you to code complex and subtle text processing that you never imagined  
could  
be automated. Regular expressions can save you time and aggravation.  
They  
can be used to craft elegant solutions to a wide range of problems.

A command of regular expressions is an invaluable skill. Yet what is  
power  
in the hands of an expert can be fraught with peril for the unwary.  
Mastering Regular Expressions will help you navigate the minefield to  
becoming an expert. Once you’ve mastered regular expressions, they’ll  
become  
an invaluable part of your toolkit. You will wonder how you ever got by  
without them.

Mastering Regular Expressions, Second Edition has been thoroughly  
updated to  
include all the new features of Perl 5.8, as well as several other  
languages, including Java, [VB.NET](http://VB.NET), C#, Python, JavaScript, Tcl, and  
Ruby.  
Written in the lucid, entertaining tone that made a complex, dry topic  
crystal-clear to thousands of programmers, and sprinkled with solutions  
to  
complex real-world problems, Mastering Regular Expressions offers a  
wealth  
information that you can put to immediate use.

Topics include:

A comparison of features among different versions of many languages and  
tools

How the regular expression engine works

Optimization (major savings available here!)

Matching just what you want, but not what you don’t want

Sections and chapters on individual languages

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 9, 2006, 2:08pm UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/8 "2006-01-09T14:08:52Z")

</div>

If you don’t have access to the O’Reilly book, here are some regex  
resources  
found on [digg.com](http://digg.com).

[http://www.crazygrrl.com/weav/reference.php3](http://www.crazygrrl.com/weav/reference.php3)

> **[Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp...](https://www.regular-expressions.info/)**
>
> At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.

[http://gnosis.cx/publish/programming/regular\_expressions.html](http://gnosis.cx/publish/programming/regular_expressions.html)

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [January 9, 2006, 10:10am UTC](https://www.ruby-forum.com/t/regular-expressions-for-beginners/51818/9 "2006-01-09T10:10:31Z")

</div>

Vladimir A. wrote:

> Eclipse that does just that. Quite useful.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

some other things that might help  
[http://www.weitz.de/regex-coach/](http://www.weitz.de/regex-coach/)  
[http://samuelfullman.com/team/php/tools/regular\_expression\_tester\_p2.php](http://samuelfullman.com/team/php/tools/regular_expression_tester_p2.php)

> **[Python re Module - Use Regular Expressions with Python - Regex Support](https://www.regular-expressions.info/python.html)**
>
> Learn how to use regular expressions with Python’s re module

but i think best is Friedl’s Oreilly regular expression book
