ruby picture

RCR 167: Add "no match expecting 'c'" to MatchData

Submitted by cyent (Wed Nov 19 17:11:39 UTC 2003)

Abstract

This is a legacy RCR from Ruby Garden, submitted by cyent. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

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, expecting a character in set [br] but found 'g' at position 3"
    

There are several ways in which this could be achieved...

  1. Rewrite a Regex engine in Ruby YUCK!
  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'
      "digby" =~ /dig(by|raph)/e
    would return false, but set some global $whatever with the MatchData

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting

Regex engine in Ruby YUCK! (neoneye2, 2003-11-19 13:22:18)

I am actually implementing a regexp engine in Ruby!

Current status:

* features of the scanner so far:
  a|b|c         alternation 
  * + ? {n,m}   repeat(min..max) greedy/lazy
  ( ... )       grouping -> register.. nested repeat also works
  .             match anything except newline
  1 .. 9      backreferences</pre> 

See:
--
Simon Strandgaard

          

This would be great (sodell, 2003-11-19 21:32:35)

I love this idea!

May I suggest a modification?

Do the /exp/e thing, but let /e denote that an exception should be thrown, and throw a custom exception which contains the match error information.


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .