Regular expressions question

On 12/14/05, Jeff W. [email protected] wrote:

You should be able to tell who this message is meant for:

PLEASE stop sending out code that uses any of the perl ${x} variables …

They are ugly and have no place in Ruby … they are only provided to
make the transition of Perl people easier …

Just to add another voice to the maelstrom: I have never coded Perl
and don’t particular like it, but I almost always use the $1 variables
in Ruby. I have been coding Ruby for over four years and I rarely find
instances where MatchData is all that much better than =~ and the $1
variables.

They are just more convenient in the typical case:

str=“abc”
re=/(.)(.)(.)/

if re =~ str
p $2
end

Versus:

if (md = re.match(str))
p md[2]
end

Ryan

Jeff W. wrote:

I’ll shut up now.

I think that’s exactly what some people want you to do. People don’t
want to be told that they’re lousy coders, and their poor practices have
only been made to work through a special case in the language
interpreter.


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

On Dec 15, 2005, at 2:12 PM, Neil S. wrote:

Jeff W. wrote:

I’ll shut up now.

I think that’s exactly what some people want you to do. People don’t
want to be told that they’re lousy coders, and their poor practices
have
only been made to work through a special case in the language
interpreter.

Hey, can we keep this to a discussion of language features and stop
insulting large portions of the Ruby community? Thank you.

James Edward G. II

On Fri, Dec 16, 2005 at 05:05:00AM +0900, Ryan L. wrote:

Just to add another voice to the maelstrom: I have never coded Perl
and don’t particular like it, but I almost always use the $1 variables
in Ruby. I have been coding Ruby for over four years and I rarely find
instances where MatchData is all that much better than =~ and the $1
variables.

They are just more convenient in the typical case:

MatchData can be quite convenient too

str = “foobar 2005-12-15”
if md = /(\S+) (\d+)-(\d+)-(\d+)/.match(str)
name, year, month, day = md.captures # => [“foobar”,
“2005”, “12”, “15”]
name # => “foobar”

end

If you want to name the captures,
md.captures
looks better than
$1, $2, $3, $4

On Dec 15, 2005, at 12:12 PM, Neil S. wrote:

Jeff W. wrote:

I’ll shut up now.

I think that’s exactly what some people want you to do. People don’t
want to be told that they’re lousy coders, and their poor practices
have
only been made to work through a special case in the language
interpreter.

Can we please try to keep it civil in here? You go code how you want
and I will go code how I want and we can both be happy.

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
[email protected]

James Edward G. II wrote:

Hey, can we keep this to a discussion of language features and stop
insulting large portions of the Ruby community? Thank you.

You are the one who first suggested that more people are using these
special-case magic variables so wantonly in their code, by bringing up
what you said in the Ruby Q…

Yeah, it’s clear that regardless of how nice the Ruby language is,
nothing can bring up the quality of the average Internet programmer,
unfortunately. Better tools don’t make a better carpenter.


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

yes, thank you. this is a better description of the problem. i am not a
native english speaker, so may be this is one of the reasons why my
question is not clear.

i saw a solution to this problem that uses split at the end. it of
course won’t work if you change your example and allow quoted strings
in source-words and destination-words. a quoted string can contain
anything, spaces too and your keywords too, so the subsequent split
won’t work.

well, i did not realise that the term “group’s captures” is that rare.
i thought it was a standard term. but may be i am brainwashed by
microsoft. so i have this code in .net which might help to clarify what
i am talking about:

        string text = "One car red car blue car";
        string pat = @"^(?:(\w+)\s+)*(\w+)$";
        Regex r = new Regex(pat, RegexOptions.IgnoreCase);

        // Match the regular expression pattern against a text

string.
Match m = r.Match(text);
if (m.Success)
{
Console.WriteLine(“match: [{0}]”, m);
foreach (Group g in m.Groups)
{
Console.WriteLine(“group: [{0}]”, g);
foreach (Capture c in g.Captures)
{
Console.WriteLine("\tcapture: [{0}]", c);
}
}
}

the output is:

match: [One car red car blue car]
group: [One car red car blue car]
capture: [One car red car blue car]
group: [blue]
capture: [One]
capture: [car]
capture: [red]
capture: [car]
capture: [blue]
group: [car]
capture: [car]

as you see, the first group is $0, the second group is $1, and the
third is $2. but $1 and $2 contain captures too. it is like if $1 and
$2 were arrays in Ruby.

in my opinion this is a big limitation of ruby’s regular expressions.
it just must be as powerful as .net ; -)

konstantin

On Dec 15, 2005, at 3:07 PM, Neil S. wrote:

James Edward G. II wrote:

Hey, can we keep this to a discussion of language features and stop
insulting large portions of the Ruby community? Thank you.

You are the one who first suggested that more people are using these
special-case magic variables so wantonly in their code, by bringing up
what you said in the Ruby Q…

Since you are educating me about the joys of MatchData, allow me to
educate you on the differences of what we both said. I explained
what I have seen and made statements about how common a given
practice is. You were, and still are, insulting people you know
nothing about.

Yeah, it’s clear that regardless of how nice the Ruby language is,
nothing can bring up the quality of the average Internet programmer,
unfortunately. Better tools don’t make a better carpenter.

You are rude for absolutely no reason and you have ceased to add
anything to this conversation. I’m done trying to reason with you.

James Edward G. II

On 12/15/05, Neil S. [email protected] wrote:

It’s unfortunate that you think it’s rude to point out facts that are
uncomfortable to you. It’s unfortunate that you’re apparently seeing
personal attacks that don’t exist.

And what “facts” have you mentioned?

To me, the magic variables fit fine with regex.

I find it really hard to understand why people are complaining about
using a little $1 after some string of absolutely cryptic pattern
matching sequences.

People tend to like Ruby’s regex system because it’s perl-like, no?

But in the interest of preserving TIMTOWTDI, there is an object
oriented solution which many posters have already mentioned.

James saying that it was common practice, and providing evidence of it
is much more reasonable than you simply insulting people you don’t
know.

A quote from Arnold in Kindergarden Cop, “Stop Whining!” applies very
well here.
You’re free to never use $1 … $n, so I don’t see what the issue is.

On Dec 15, 2005, at 1:32 PM, Neil S. wrote:

Neil S. - [email protected]
kill-filed

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
[email protected]

James Edward G. II wrote:

You are rude for absolutely no reason and you have ceased to add
anything to this conversation. I’m done trying to reason with you.

It’s unfortunate that you think it’s rude to point out facts that are
uncomfortable to you. It’s unfortunate that you’re apparently seeing
personal attacks that don’t exist.


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

Ezra Z. wrote:

Can we please try to keep it civil in here? You go code how you want
and I will go code how I want and we can both be happy.

Actually, no, you’re wrong. String’s gsub, for one, has been hard-wired
to force you to use magic punctuation in order to access the match data.

This forces everyone to depend on the pseudo-global variable hacks
unless they run their Regexp multiple times:

str = ‘root:*:0:0:System A.:/var/root:/bin/sh’
re = /([^:]+)(:|$)/
str.gsub!(re) do |m|
matchData = re.match(m)
‘x’ * matchData[1].length + matchData[2]
end
puts str
=> ‘xxxx:x:x:x:xxxxxxxxxxxxxxxxxxxx:xxxxxxxxx:xxxxxxx’

So the langauge is hard-coded to force everyone to use pseudo-global
magically-scoped variables, that you have to cross your fingers and hope
work the way you need at the moment.


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

Neil S. wrote:

Actually, no, you’re wrong.

plonk

I know I said I’d shut up, and I am, but I did feel that after some of
the messages that have popped since mine, that I should have a final
comment…

I do apologize to you all for causing such grief. I was simply trying
to add my $0.02 as what I thought was best practice … and made the
assumption that most people were doing things that way ( I don’t use
the ${x} vars at all, but I bet you knew that already )…

I’m not saying that people are bad coders for using those, I didn’t
think I was being insulting in my OP. I was simply suggesting that I
thought it a better practice to show the most oop way of doing things
in code sent to newer programmers …

Truly, if anybody took offense, it was unintentional. If you knew me
better, you would understand that I tend to be passionate about things,
especially when it comes to teaching the next gen of programmers and/or
users of Ruby…

So, peace, love, and joyful programming to you all, I will try to be
more cautious with my posts in the future.

j.

On Dec 15, 2005, at 7:47 PM, [email protected] wrote:

I do apologize to you all for causing such grief.

I too apologize if I came back at you too harsh. It was unintentional.

Group hug everyone! :wink:

James Edward G. II

Hi –

On Fri, 16 Dec 2005, Neil S. wrote:

Ezra Z. wrote:

Can we please try to keep it civil in here? You go code how you want
and I will go code how I want and we can both be happy.

Actually, no, you’re wrong. String’s gsub, for one, has been hard-wired
to force you to use magic punctuation in order to access the match data.

This forces everyone to depend on the pseudo-global variable hacks
unless they run their Regexp multiple times:

You can minimize it by using $~.

David


David A. Black
[email protected]

“Ruby for Rails: Ruby techniques for Rails developers”,
coming April 2006 (Ruby for Rails)

Ryan L. wrote:

On 12/15/05, [email protected] [email protected] wrote:

I do apologize to you all for causing such grief.

You have been fairly civil. What I find most amusing is how Neil
started the flame-fest by agreeing to your self-imposed shutting up,
then ended up having the same opinion as you (that the $1 variables
are bad.) Oh the irony.

Actually, I think you should read more carefully what I wrote. The
irony here isn’t what you think.


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

On 12/15/05, Neil S. [email protected] wrote:

Actually, I think you should read more carefully what I wrote. The
irony here isn’t what you think.

Yes I see. Actually your statement can be interpreted either way,
based on context. Since I hadn’t read the thread for several hours I
forgot which side of the debate you were on. So, when I read this:

“I think that’s exactly what some people want you to do. People don’t
want to be told that they’re lousy coders, and their poor practices have
only been made to work through a special case in the language
interpreter.”

It sounded like you wanted Jeff to shut up, and that you were in the
group being called lousy coders and you didn’t like it. But now I see
you were talking about other people in the nasty way shown all over
this thread.

I don’t know if you will fit in the Ruby community because we are
generally a nice bunch of people and your behavior on this thread
hasn’t been very nice. In case the life lesson hasn’t been taught to
you yet, you attract more flies with honey than with vinegar, so to
speak.

Regards,
Ryan

On 12/15/05, [email protected] [email protected] wrote:

I do apologize to you all for causing such grief.

You have been fairly civil. What I find most amusing is how Neil
started the flame-fest by agreeing to your self-imposed shutting up,
then ended up having the same opinion as you (that the $1 variables
are bad.) Oh the irony.

Ryan

Ryan L. wrote:

I don’t know if you will fit in the Ruby community because we are
generally a nice bunch of people and your behavior on this thread
hasn’t been very nice. In case the life lesson hasn’t been taught to
you yet, you attract more flies with honey than with vinegar, so to
speak.

I never called out anyone in particular, didn’t have anyone in mind in
fact, but it’s funny how some people are insecure enough that they’re
getting offended by what I wrote, and getting so defensive that they
feel the need to lash out in return.

It reminds me of a saying: “If you throw a rock into a pack of dogs, the
dog that yelps loudest is the one that got hit.”


Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin