How to Convert Regex Back to String in Exact Match? 
Author Message
 How to Convert Regex Back to String in Exact Match?

Hi,

Some time ago, I posted the question on "How to Convert String to Regex to
Perform Exact Match".  The problem is to generate a regex "reg" from
string "str" such that these two are equivalent:

    someString == str

    someString =~ reg

and the answer is

    reg = Regexp.new('\A' + Regexp.escape(str) + '\z')

Now, given a regex "reg" that was created from a string "str" such as
above, is there any simple way to recover str back from reg?  I know that
in general it is impossible to convert a regex into a string.  However, in
the special case such as above (we know how reg was created from str), is
there an easy way to convert reg to str?

Basically, I need the inverse of the method escape.  I can remove '\A'
and '\z' without any problem, but I don't know how to deal with all the
replacements that have been done by the method escape.  Thanks.

Regards,

Bill



Mon, 01 Nov 2004 01:25:38 GMT  
 How to Convert Regex Back to String in Exact Match?
Hi,

At Thu, 16 May 2002 02:39:56 +0900,

Quote:

> Now, given a regex "reg" that was created from a string "str" such as
> above, is there any simple way to recover str back from reg?  I know that
> in general it is impossible to convert a regex into a string.  However, in
> the special case such as above (we know how reg was created from str), is
> there an easy way to convert reg to str?

Keeping original string. :)

Quote:
> Basically, I need the inverse of the method escape.  I can remove '\A'
> and '\z' without any problem, but I don't know how to deal with all the
> replacements that have been done by the method escape.  Thanks.

Strip backslashes followed by any letter.

  reg.source.sub(/\A\\A/, '').sub(/\\z\z/, '').gsub(/\\(.)/, '\1')

--
Nobu Nakada



Mon, 01 Nov 2004 07:17:46 GMT  
 How to Convert Regex Back to String in Exact Match?

Quote:

> the special case such as above (we know how reg was created from str), is
> there an easy way to convert reg to str?

Untested && unsporting && wasteful code follows, taking lead from Nobu's
suggestion to save the string -- let the class do it for you...

class Regexp
  alias old_escape escape
  def Regexp.escape(string)
    escaped = old_escape(string)


    return escaped
  end
  def Regexp.unescape(regexp)

  end
end

;)

 -- Nikodemus



Mon, 01 Nov 2004 07:38:37 GMT  
 How to Convert Regex Back to String in Exact Match?
Hello --

Quote:


> > the special case such as above (we know how reg was created from str), is
> > there an easy way to convert reg to str?

> Untested && unsporting && wasteful code follows, taking lead from Nobu's
> suggestion to save the string -- let the class do it for you...

> class Regexp
>   alias old_escape escape
>   def Regexp.escape(string)
>     escaped = old_escape(string)


>     return escaped
>   end
>   def Regexp.unescape(regexp)

>   end
> end

Too much work :-)  Here's a simpler, Nobu-derived way:

  class << Regexp
    def unescape(re)
      re.source.sub(/\A\\A/, '').sub(/\\z\z/, '').gsub(/\\(.)/, '\1')
    end
  end

I think this is better, design-wise, than storing them in a
(class-wide) hash, because the hash mixes per-object intelligence and
class functionality.  (Mind you, I haven't really thought through the
nuances of any of this... just thinking out loud.)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 01 Nov 2004 08:47:02 GMT  
 How to Convert Regex Back to String in Exact Match?

Quote:

>       re.source.sub(/\A\\A/, '').sub(/\\z\z/, '').gsub(/\\(.)/, '\1')

Nice ;) Now just bundle it up with unit tests... Funny though how
our versions are very much about size vs. work. Your is much
more elengant, though.

 -- Nikodemus



Mon, 01 Nov 2004 09:13:03 GMT  
 How to Convert Regex Back to String in Exact Match?
Hi --

Quote:


> >       re.source.sub(/\A\\A/, '').sub(/\\z\z/, '').gsub(/\\(.)/, '\1')

> Nice ;) Now just bundle it up with unit tests... Funny though how
> our versions are very much about size vs. work. Your is much
> more elengant, though.

The above line was cut-and-pasted from Nobu (just to give credit
where it's due :-)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Mon, 01 Nov 2004 09:18:15 GMT  
 How to Convert Regex Back to String in Exact Match?
Hi,

In message "How to Convert Regex Back to String in Exact Match?"

|Basically, I need the inverse of the method escape.  I can remove '\A'
|and '\z' without any problem, but I don't know how to deal with all the
|replacements that have been done by the method escape.  Thanks.

I think

  s.gsub(/\\(.)/, '\1')

does the trick.

                                                        matz.



Mon, 01 Nov 2004 10:25:57 GMT  
 How to Convert Regex Back to String in Exact Match?
Hi Bill,
     Well, its not a very elegant way to do it, but with the assumption
that saving alot of your time is worth more than saving a little bit of
computer memory, I'd probably add a couple of new methods to the RegExp
class called something like Regexp::save_originating_string and
Regexp::recover_originating_string.  Then, each time you call
Regexp::new(string), you can follow it with a call to
Regexp::save_originating_string(string), passing the same string to both.
If you want to be slightly more elegant, and cut down on possibilities for
human error, you could create a new class called (for example) RRegexp,
which would be a subclass of Regexp.  (yes, I don't much like
that name either, but I'm too lazy or unimaginative to think of
a good one.  I'm sure you can come up with a much better one on
your own).  In any case, RRegexp.new would squirrel away its argument in
an instance variable, and then call self.super, returning the results to
the caller (well, message sender in OO terms).  In this case, you would
not need the save_originating_string method, but just the
recover_originating_string method.  Hope this helps.

Dennis

Quote:

> Hi,

> Some time ago, I posted the question on "How to Convert String to Regex to
> Perform Exact Match".  The problem is to generate a regex "reg" from
> string "str" such that these two are equivalent:

>     someString == str

>     someString =~ reg

> and the answer is

>     reg = Regexp.new('\A' + Regexp.escape(str) + '\z')

> Now, given a regex "reg" that was created from a string "str" such as
> above, is there any simple way to recover str back from reg?  I know that
> in general it is impossible to convert a regex into a string.  However, in
> the special case such as above (we know how reg was created from str), is
> there an easy way to convert reg to str?

> Basically, I need the inverse of the method escape.  I can remove '\A'
> and '\z' without any problem, but I don't know how to deal with all the
> replacements that have been done by the method escape.  Thanks.

> Regards,

> Bill



Tue, 02 Nov 2004 01:36:21 GMT  
 How to Convert Regex Back to String in Exact Match?
Thanks for all the replies.  I conclude that the solution of

  re = Regexp.new('\A' + Regexp.escape(str) + '\z') # (1)

is

  str = re.source.sub(/\A\\A/,'').sub(/\\z\z/,'').gsub(/\\(.)/,'\1') # (2)

However, as already pointed out by several people, I also infer that
probably it is a very good idea to store the original string, because when
I just invoke (2), I actually have little clue whether the regex was
originally created using (1), and when it is actually not, I have not much
control on what (2) will give me.  At least, I learn a very useful Ruby
(or simply Regexp) "trick".  Thanks, everyone.

Regards,

Bill



Tue, 02 Nov 2004 23:29:49 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How to Convert String to Regex to Perform Exact Match

2. how to do exact match for all characters using re.match()

3. Converting object names to strings and back?

4. converting list back to string

5. String Match - not matching

6. Store regex match in a variable

7. regex matching and text moving

8. Regex \w does not match underscore

9. regex: how to limit pattern match to first occurance

10. RegEx ASCII character matching

11. Need regex help (or bug in match)

12. Scope of regex match $variables

 

 
Powered by phpBB® Forum Software