Ruby Regexp Appendix
Tags: ruby, regex, ebook, tools
Published: 2021-01-15 11:41:10

---
title: "Ruby Regexp Appendix"
published: true
description: "Learn Ruby regular expressions from basics to advanced levels with plenty of examples and exercises"
series: Ruby Regexp
tags: ruby,regexp,ebook,tools
canonical_url: https://learnbyexample.github.io/Ruby_Regexp/interlude-tools-for-debugging-and-visualization.html
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/dt3kscnnmrlnvmzu05wv.png
---

# Tools for debugging and visualization

As your regexp gets complicated, it can get difficult to debug if you run into issues. Building your regexp step by step from scratch and testing against input strings will go a long way in correcting the problem. To aid in such a process, you could use [various online tools](https://news.ycombinator.com/item?id=20614847).

## rubular

[rubular](https://rubular.com/) is an online Ruby regular expression editor (based on Ruby 2.5.7) to visually test your regexp. You need to add your regexp, input string and optional modifiers. Matching portions will be highlighted.

The below image is a screenshot from this link — [rubular: `/ab{0,2}c/`](https://rubular.com/r/TkZiW5fx8WucUS)

![rubular example](https://learnbyexample.github.io/Ruby_Regexp/images/rubular.png)

>[regex101](https://regex101.com/) and [regexr](https://regexr.com/) are similar sites with more features, but they do not support Ruby flavor. They both have JavaScript flavor, which is the closest option to Ruby.

## debuggex

Another useful tool is [debuggex](https://www.debuggex.com/) which converts your regexp to a rail road diagram, thus providing a visual aid to understanding the pattern. This doesn't support Ruby, so select JavaScript flavor.

The below image is a screenshot from this link — [debuggex: `/\bpar(en|ro)?t\b/`](https://www.debuggex.com/r/YvhfAn46M4G3_C0i)

![debuggex example](https://learnbyexample.github.io/Ruby_Regexp/images/debuggex.png)

## regexcrossword

For practice, [regexcrossword](https://regexcrossword.com/) is often recommended. It only supports JavaScript, so some of the puzzles may not work the same with Ruby syntax. See [regexcrossword: howtoplay](https://regexcrossword.com/howtoplay) for help.

The below image is a screenshot from this link — [regexcrossword: tutorial puzzle 5](https://regexcrossword.com/challenges/tutorial/puzzles/5)

![regexcrossword example](https://learnbyexample.github.io/Ruby_Regexp/images/regexcrossword.png)

# Common tasks

Tasks like matching phone numbers, ip addresses, dates, etc are so common that you can often find them collected as a library. This chapter shows some examples for [CommonRegexRuby](https://github.com/talyssonoc/CommonRegexRuby). See also [Awesome Regex: Collections](https://github.com/aloisdg/awesome-regex#collections).

## CommonRegexRuby

You can either install `commonregex` gem or go through [commonregex.rb](https://github.com/talyssonoc/CommonRegexRuby/blob/master/lib/commonregex.rb) and choose the regular expression you need. See also [CommonRegexRuby: README](https://github.com/talyssonoc/CommonRegexRuby/blob/master/README.md) for details and examples of available patterns.

```ruby
>> require 'commonregex'
=> true

>> data = 'hello 255.21.255.22 okay 23/04/96'

# match all available patterns
>> parsed = CommonRegex.new(data)
>> parsed.get_ipv4
=> ["255.21.255.22"]
>> parsed.get_dates
=> ["23/04/96"]

# or, use specific method directly on CommonRegex
>> CommonRegex.get_ipv4(data)
=> ["255.21.255.22"]
>> CommonRegex.get_dates(data)
=> ["23/04/96"]
```

Make sure to test these patterns for your use case. For example, the below data has a valid IPv4 address followed by another number separated by a dot character. If such cases should be ignored, then you'll have to create your own version of the pattern or change the input accordingly.

```ruby
>> new_data = '23.14.2.4.2 255.21.255.22 567.12.2.1'

# 23.14.2.4 gets matched from 23.14.2.4.2
>> CommonRegex.get_ipv4(new_data)
=> ["23.14.2.4", "255.21.255.22"]
```