Regular expressions alternative

Hi,

Wouldn’t it be a good idea to create a option (say, b) for regular
expressions such that all characters (except / and ) would match
themselves unless preceeded by a backslash? For instance:

/http://www.ruby-lang.org/b

would match only the site name, and

/Something$/b

would match Something at an end of line?

[…],
Maurício

you’re essentially reversing the semantics of meta characters. i don’t know
whether people will appreciate this since this can lead to wired errors,
when you don’t realize the option is set. why not just write a function
that escapes all meta characters?

robert

“Maurício” briqueabraque@yahoo.com schrieb im Newsbeitrag
news:aveea7$3gg$1@main.gmane.org

···

Hi,

Wouldn’t it be a good idea to create a option (say, b) for regular
expressions such that all characters (except / and ) would match
themselves unless preceeded by a backslash? For instance:

/http://www.ruby-lang.org/b

would match only the site name, and

/Something$/b

would match Something at an end of line?

[…],
Maurício

Here’s an alternative implementation. Usage:

a = Regexp.nonmagic(‘string’) # converts ‘string’ to a normal regexp

The advantage is that you can now use a.source to embed your regexp into
a larger regexp and have the larger regexp be normal (if you’re a vi
user you know how annoying having to escape metacharacters can be).

–begin–

class Regexp
class << self
def nonmagic(s)
special = Regexp.compile(‘[1]$’
)
r = s.scan(/(\.|.)/).flatten.map {|i|
case i
when ‘\\’; Regexp.quote(‘\’)
when /(\(.))/
p, q = $1, $2
(p =~ special) ? p : q
else; Regexp.quote(i)
end
}.join
Regexp.compile(r)
end
end
end

–end–

···

Maur?cio briqueabraque@yahoo.com wrote:

Hi,

Wouldn’t it be a good idea to create a option (say, b) for regular
expressions such that all characters (except / and ) would match
themselves unless preceeded by a backslash? For instance:


  1. ‘+’.|()^{}+$*?‘.split.map{|i|
    Regexp.quote(i)}.join+’ ↩︎

when you don’t realize the option is set. why not just write a function
that escapes all meta characters?

Because Regexp.escape() === Regexp.quote() already exists? :slight_smile:

robert
    Hugh
···

On Tue, 7 Jan 2003, Robert Klemme wrote:

Hi,

Wouldn’t it be a good idea to create a option (say, b) for regular
expressions such that all characters (except / and ) would match
themselves unless preceeded by a backslash? For instance:

/http://www.ruby-lang.org/b

would match only the site name (…)

you’re essentially reversing the semantics of meta characters.

I think I’m only standardizing. a matches a, so, $ matches $.
If you want something special, preceed it with a backslash. I
think that should have been done in the first place.

this can lead to wired errors, when you don’t realize the option
is set.

This can happen with any of the existing options, i, x and o…

why not just write a function that escapes all
meta characters?

I want to be able to create a regular expression without
knowing by heart all existing special characters. I would
like, for instance, to write a regexp that matches
100% (or any number like \d\d%) without knowing if % is
a special character or not. Even the existing function
Regexp.escape doesn’t help with that.

[…],
Maurício

I think I’m only standardizing. a matches a, so, $ matches $.
If you want something special, preceed it with a backslash. I
think that should have been done in the first place.

Most things are done for a reason, and this is no exception. Regex in
Vim is done mostly the way you’re talking about, and it is frustrating
getting lost in a sea of backslashes all the time.

I want to be able to create a regular expression without
knowing by heart all existing special characters. I would
like, for instance, to write a regexp that matches
100% (or any number like \d\d%) without knowing if % is
a special character or not. Even the existing function
Regexp.escape doesn’t help with that.

irb can help, though :slight_smile:

irb(main):001:0> “89%” =~ /(\d+)%/
=> 0
irb(main):002:0> $1
=> “89”

Gavin

···

On Wednesday, January 8, 2003, 2:26:53 AM, Maurício wrote:

(…)

I want to be able to create a regular expression without
knowing by heart all existing special characters. (…)>

irb can help, though :slight_smile:
(…)

Well, I think the ‘b’ option I suggested is easier. But
I won’t flame on that :slight_smile:

[…],
Maurício

I am using a dateTime field in mysql, the default value of which is
0000-00-00 00:00:00, and I am running into the a problem with
dbi/sql.rb, my version of which reads $Id: sql.rb,v 1.12 2002/02/06
14:24:21 in the file header. The problem and (a duplicatable method of
generating it) is as follows:

Say I have a database table called ‘ppl’ that looks like this:

PGP.sig (186 Bytes)

···

±-------±---------------------±----±--------------------
±---------------+

Field | Type | Key | Default | Extra

±-------±---------------------±----±--------------------
±---------------+

ID | smallint(5) unsigned | PRI | NULL |
auto_increment |
name | char(8) | | |

mdate | datetime | | 0000-00-00 00:00:00 |

±-------±---------------------±----±--------------------
±---------------+

Then I insert a row as follows:

insert into ppl (name) values (‘tom’)

I’ll get the following row:

±—±-----±--------------------+

ID | name | mdate |
±—±-----±--------------------+
1 | Tom | 0000-00-00 00:00:00 |
±—±-----±--------------------+

Now, say I try to access this via ruby and load in into an array of
DBI::Rows as follows:

dbh = DBI.connect(dsn, user, auth, params)
a = Array.new()

ssh = dbh.execute(“select ID, name, mdate from ppl”)
sth.each { |val| a << val }

sth.finish
dbh.disconnect

Executing this gets the following error:

/usr/lib/site_ruby/1.6/dbi/sql.rb:59:in gm': argument out of range (ArgumentError) from /usr/lib/site_ruby/1.6/dbi/sql.rb:59:in as_timestamp’
from /usr/lib/site_ruby/1.6/dbi/sql.rb:79:in send' from /usr/lib/site_ruby/1.6/dbi/sql.rb:79:in coerce’
from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:377:in
fill_array' from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:374:in each_with_index’
from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:374:in each' from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:374:in each_with_index’
from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:374:in
fill_array' from /usr/lib/site_ruby/1.6/DBD/Mysql/Mysql.rb:384:in fetch’
from /usr/lib/site_ruby/1.6/dbi/dbi.rb:786:in fetch' from /usr/lib/site_ruby/1.6/dbi/dbi.rb:811:in each’

There erroneous line in “/usr/lib/site_ruby/1.6/dbi/sql.rb” is:

     time = ::Time.gm(*(ary[0,6]))

It appears that the problem is being caused by the zero values in the
mdate field. I changed the mdate field as follows:

update ppl set mdate=“2003-1-2 12:16:31”

And this fixed the problem.


David King Landrith
(w) 617.227.4469x213
(h) 617.696.7133

One useless man is a disgrace, two
are called a law firm, and three or more
become a congress – John Adams

public key available upon request