DEV Community

Cover image for Ruby Regexp Appendix

Posted on • Originally published at

Ruby Regexp Appendix

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.


rubular 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/

rubular example

regex101 and regexr 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.


Another useful tool is debuggex 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/

debuggex example


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

The below image is a screenshot from this link — regexcrossword: tutorial puzzle 5

regexcrossword example

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. See also Awesome Regex: Collections.


You can either install commonregex gem or go through commonregex.rb and choose the regular expression you need. See also CommonRegexRuby: README for details and examples of available patterns.

>> require 'commonregex'
=> true

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

# match all available patterns
>> parsed =
>> parsed.get_ipv4
=> [""]
>> parsed.get_dates
=> ["23/04/96"]

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

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.

>> new_data = ' 567.12.2.1'

# gets matched from
>> CommonRegex.get_ipv4(new_data)
=> ["", ""]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)