Need a regex

R

Randy Harris

I would appreciate help creating a regex. I have a file that contains lines
that begin with several groups of numbers separated by a dash followed by a
dash and then non digit data. For example:

123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

I need to capture the numeric portion, up to but not including the last
dash. From the above samples I would want:

123-12-23
561-318-001
453-2193

Thank you for any help.
 
S

Shane

I would appreciate help creating a regex. I have a file that contains
lines that begin with several groups of numbers separated by a dash
followed by a dash and then non digit data. For example:

123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

I need to capture the numeric portion, up to but not including the last
dash. From the above samples I would want:

123-12-23
561-318-001
453-2193

Thank you for any help.

my @list = <Filehandle>
$list[$linenumber] =~ /([\d-]+)/
print $1

HTH
 
A

A. Sinan Unur

To the OP: This is not a "write my code for me" service. If you do not
put any effort into helping others help you, you cannot expect others to
put much effort into helping you.

Or, you get:

use strict;
use warnings;

missing.
my @list = <Filehandle>
$list[$linenumber] =~ /([\d-]+)/
print $1

Now, Shane,

1. Why are you slurping the whole file only to process it line by line?
2. What is $linenumber?
3. Since you are not actually looping through all the elements of @list,
do you really expect this to do what the OP asked for?
4. You are using $1 without checking if the regex actually matched.
5. Since you did not anchor the regex, it will also match lines such as

Hello -555- World!

This is clearly against the specs.

6. The regex you specified will also match the trailing dash. Again, the
OP was asking the exact opposite.

Here is something that will actually work:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
if( /^([\d-]+\d+)-/ ) {
print "$1\n";
}
}

__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

D:\Home\asu1\UseNet\clpmisc> www
123-12-23
561-318-001
453-2193

Please read the posting guidelines for this group.

Sinan
 
S

Shane

Or, you get:

use strict;
use warnings;

missing.
my @list = <Filehandle>
$list[$linenumber] =~ /([\d-]+)/
print $1

Now, Shane,

1. Why are you slurping the whole file only to process it line by line? 2.
What is $linenumber?
3. Since you are not actually looping through all the elements of @list,
do you really expect this to do what the OP asked for? 4. You are using $1
without checking if the regex actually matched. 5. Since you did not
anchor the regex, it will also match lines such as

Hello -555- World!

This is clearly against the specs.

6. The regex you specified will also match the trailing dash. Again, the
OP was asking the exact opposite.

Here is something that will actually work:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
if( /^([\d-]+\d+)-/ ) {
print "$1\n";
}
}
}
__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

D:\Home\asu1\UseNet\clpmisc> www
123-12-23
561-318-001
453-2193

Please read the posting guidelines for this group.

Sinan

who says this isnt wrong :
"The best way to get the right answer on usenet is to post the wrong one." :)
 
R

Randy Harris

A. Sinan Unur said:
To the OP: This is not a "write my code for me" service. If you do not
put any effort into helping others help you, you cannot expect others to
put much effort into helping you.

Or, you get:

use strict;
use warnings;

missing.
my @list = <Filehandle>
$list[$linenumber] =~ /([\d-]+)/
print $1

Now, Shane,

1. Why are you slurping the whole file only to process it line by line?
2. What is $linenumber?
3. Since you are not actually looping through all the elements of @list,
do you really expect this to do what the OP asked for?
4. You are using $1 without checking if the regex actually matched.
5. Since you did not anchor the regex, it will also match lines such as

Hello -555- World!

This is clearly against the specs.

6. The regex you specified will also match the trailing dash. Again, the
OP was asking the exact opposite.

Here is something that will actually work:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
if( /^([\d-]+\d+)-/ ) {
print "$1\n";
}
}

__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

D:\Home\asu1\UseNet\clpmisc> www
123-12-23
561-318-001
453-2193

Please read the posting guidelines for this group.

Sinan

Thank you. I'll attempt to adhere more closely to the posting guidelines.
I have been reading the docs, it was mostly the grouping for the quantifier
that I couldn't figure out. [ ]
 
T

Tad McClellan

Randy Harris said:
followed by a
dash and then non digit data. For example:

123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

I need to capture the numeric portion, up to but not including the last
dash. From the above samples I would want:

123-12-23
561-318-001
453-2193


--------------------
#!/usr/bin/perl
use warnings;
use strict;

while ( <DATA> ) {
print "$1\n" if /(.*)-/;
}

__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose
 
J

John Bokma

Randy Harris said:
I would appreciate help creating a regex. I have a file that contains
lines that begin with several groups of numbers separated by a dash
followed by a dash and then non digit data. For example:

123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

Are there non-fitting hoses as well?
 
W

William James

Randy said:
I would appreciate help creating a regex. I have a file that contains lines
that begin with several groups of numbers separated by a dash followed by a
dash and then non digit data. For example:

123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

I need to capture the numeric portion, up to but not including the last
dash. From the above samples I would want:

123-12-23
561-318-001
453-2193

Thank you for any help.

It's easy in Ruby:

puts DATA.to_a.map{ |x| x[ /^([-\d]*)-/, 1 ] }

__END__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose
 
T

Tom Potter

To the OP: This is not a "write my code for me" service. If you do not
put any effort into helping others help you, you cannot expect others to
put much effort into helping you.

Or, you get:

use strict;
use warnings;

missing.
my @list = <Filehandle>
$list[$linenumber] =~ /([\d-]+)/
print $1

Now, Shane,

1. Why are you slurping the whole file only to process it line by line?
2. What is $linenumber?
3. Since you are not actually looping through all the elements of @list,
do you really expect this to do what the OP asked for?
4. You are using $1 without checking if the regex actually matched.
5. Since you did not anchor the regex, it will also match lines such as

Hello -555- World!

This is clearly against the specs.

6. The regex you specified will also match the trailing dash. Again, the
OP was asking the exact opposite.

Here is something that will actually work:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
if( /^([\d-]+\d+)-/ ) {
print "$1\n";
}
}

__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

D:\Home\asu1\UseNet\clpmisc> www
123-12-23
561-318-001
453-2193

Please read the posting guidelines for this group.

Sinan

Please don't take it otherwise,
I would never understand why pepople act so pricy in answering queries for
a freeware like perl, look at Tad's answer, short and simple, few maybe
more people don't do serious programing in perl ,they do it cause they
think its better,efficient and easy then writing a batch file, why do such
persons get a arrogant answer or anybody,
 
A

Anno Siegel

A. Sinan Unur said:
[...]

Here is something that will actually work:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>) {
if( /^([\d-]+\d+)-/ ) {
print "$1\n";
}
}

__DATA__
123-12-23-Bottom Bracket
561-318-001-Cowling Spacer 2
453-2193-Fitting Hose

D:\Home\asu1\UseNet\clpmisc> www
123-12-23
561-318-001
453-2193

....or a regex-reduced version:

print join( '-', grep !/\D/, split /-/), "\n" while <DATA>;

Anno
 
T

Tad McClellan

[ snip silly (I'd say trollish) code ]



[ snip a litany of the silliness ]



[ snip answer to OP's question! ]



Always good advice.

Please don't take it otherwise,


That sentence makes no sense, unless you indicate other than *what*.

I think you are hoping to stay out of scorefiles, but I must say that
you are teetering on the brink of getting in anyway.

I would never understand why pepople act so pricy in answering queries


Maybe it is because you don't have the same context and experiences
as those you are observing.

Have you been observing here daily for months or years?

for
a freeware like perl,


Perl (and perl) is _better_ than freeware.

Perl is open source, it is _both_ kinds of "free".


I fail to see how the business model of the subject would make a
difference in what to expect in a Usenet newsgroup about that subject.

Are folks "nice" in some other high-traffic newsgroup that discusses
some "closed" software?

Usenet is not a help desk, even when the group's topic is a
commercial program.

look at Tad's answer, short and simple,


I had decided not to respond to the OP for exactly the reason
given in ASU's "To the OP" note above.

It was only after seeing several followups that failed to notice
the possible simplification of the requirements that I had seen
did I decide to post to this thread.

I posted for the benefit of the thread's readers rather than for the OP.

few maybe
more people don't do serious programing in perl ,they do it cause they
think its better,efficient and easy then writing a batch file,


That is a primary reason underlying the angst that we have here in
clp.misc versus most other comp.lang.* newsgroups.

Some here are (expert?) professional programmers while some are not.

Perl is easy enough to use that hobbyists can get things done with it.

When the hobbyist can't get "Hello world" to run in Java or C++, they
give up, so the proportion of professional programmers is much higher
in their newsgroups than it is here.

Some impedance mismatch is to be expected when groups from opposite
ends of a spectrum interact with each other.

why do such
persons get a arrogant answer or anybody,


Arrogant?

What, exactly, did you see as arrogant?

Was it ASU's reply to the OP or to Shane?

We can't tell because you did a full-quote.



You know what I see as arrogant?

Someone who just got here (as far as I can tell) telling us
how to do it.
 
A

A. Sinan Unur

....

while ( <DATA> ) {
print "$1\n" if /(.*)-/;
}

Now, I had not thought of that :)

This would absolutely do what the OP asked for when the data contained
exactly what he showed.

On the other hand, (maybe I'm thinking too much in terms of processing CGI
form submissions these days) I try to write my regexes to match only the
things I want them to match.

Thanks,

Sinan
 
W

William James

Randy said:
What could I have done differently to ask for help without incurring the
wrath of regulars such as yourself and Tad?

Please post the same question on comp.lang.ruby. You will receive
a much more polite reception.
 
A

A. Sinan Unur

What could I have done differently to ask for help without incurring
the wrath of regulars such as yourself and Tad?

You did not incur anyone's wrath.

The easy way of getting help here is outlined in the posting guidelines.
In short, you try something, you post what you have tried, explain how
it does not do what you want it to do.

Postings without code generally get ignored unless someone like Shane
actually posts a bogus answer. In that case, I feel compelled to point
out the errors.

So, you could have posted a short script that others can run by copying
and pasting into an editor. See the posting guidelines to learn how to
do that. Oh, and please, no line numbers. I try to ignore code with line
numbers as much as possible. Posting code with line numbers is probably
a good way to ensure that no one is going to run, or play with your code
to find an answer.

In short, read the posting guidelines.

Sinan
 
W

William James

Randy said:
What could I have done differently to ask for help without incurring the
wrath of regulars such as yourself and Tad?

If you'll post your original problem on comp.lang.ruby, you'll find
that the people there are much more polite (as well as being better
programmers). After using Ruby, you'll conclude that it's a better
language.
 
D

David H. Adler

If you'll post your original problem on comp.lang.ruby, you'll find
that the people there are much more polite (as well as being better
programmers). After using Ruby, you'll conclude that it's a better
language.

Every once in a while I start thinking that your constant posting about
ruby in clpmisc is just the result of misguided zeal.

Thanks for reminding me that you are just a troll.

dha
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top