Editing Topic: RCR179 Project: RCR | RCRchive home

RCR 179: Create a 'NoMatchData' object on Regex match failing.

submitted by cyent on Mon Jan 05 2004 12:42:15 PM -0800

Status: pending


Abstract

Extend MatchData to have information on why a Regular Expression match failed.

Problem

How often have you written a Regex and expected it to match something and it just didn't? You got it wrong, but you have no idea how. Or worse, your user has entered input wrong, and you can't tell him what, just that it is wrong. Any sensible parser when choking on input will tell you, 'Syntax Error: Expecting one of "blah,bloo,foo"', so why can't Ruby Regex's? For example if I try match "digger" =~ /dig(by|raph)/ expecting to match either digby or digraph it would be nice if Ruby could tell me, "No match, longest match "dig", expecting a character in set [br] but found 'g' at position 3"

Proposal

There are several ways in which this could be achieved... 1. Rewrite a Regex engine in Ruby. 2. Change Regex::match to always return a MatchData that held the info. This would break some existing code. 3. Add a new method Regex::try_match(string) which would always return a MatchData, even if it didn't match. 4. Add a new modifier 'e' "digger" =~ /dig(by|raph)/e would return false, but set some global $whatever with the MatchData

Analysis

Option 1 would probably be too slow, option 2 would break too much existing code. I would favour option 3 or 4.


Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog