Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help with a regex

Reply
Thread Tools

help with a regex

 
 
donebrowsers
Guest
Posts: n/a
 
      03-12-2008
I have the following dataset:
zoo-2.10.1p1
mutt-1.4.2.3-compressed
lha-1.14i.ac20050924.1
mysql-server-5.0.45
p5-Archive-Tar-1.30
php5-gd-5.2.3-no_x11

There are package listings on an OpenBSD machine. I want to parse the
package name, the version, and if there is a flavor, that too. I want
the following:
[zoo] [2.10.1p1]
[mutt] [1.4.2.3] [compressed]
[lha] [1.14i.ac20050924.1]
[mysql-server] [5.0.45]
[p5-Archive-Tar] [1.30]
[php5-gd] [5.2.3] [no_x11]

I currently have this regex (which is close but doesn't quite work):
/(^.*)-(.*)($-\w)?/

It currently gives me:
[zoo] [2.10.1p1]
[mutt-1.4.2.3] [compressed]
[lha] [1.14i.ac20050924.1]
[mysql-server] [5.0.45]
[p5-Archive-Tar] [1.30]
[php5-gd-5.2.3] [no_x11]

As you can see I'm having issues with the flavor (the last part of the
package name which not every package has) part. Any help?
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      03-12-2008
donebrowsers wrote:
> I have the following dataset:
> zoo-2.10.1p1
> mutt-1.4.2.3-compressed
> lha-1.14i.ac20050924.1
> mysql-server-5.0.45
> p5-Archive-Tar-1.30
> php5-gd-5.2.3-no_x11
>
> There are package listings on an OpenBSD machine. I want to parse the
> package name, the version, and if there is a flavor, that too. I want
> the following:
> [zoo] [2.10.1p1]
> [mutt] [1.4.2.3] [compressed]
> [lha] [1.14i.ac20050924.1]
> [mysql-server] [5.0.45]
> [p5-Archive-Tar] [1.30]
> [php5-gd] [5.2.3] [no_x11]


$ echo "zoo-2.10.1p1
mutt-1.4.2.3-compressed
lha-1.14i.ac20050924.1
mysql-server-5.0.45
p5-Archive-Tar-1.30
php5-gd-5.2.3-no_x11" | \
perl -lne'
print join " ", map $_ ? "[$_]" : (), split /-?(\d[\w.]*\d)-?/, $_, 2
'
[zoo] [2.10.1p1]
[mutt] [1.4.2.3] [compressed]
[lha] [1.14i.ac20050924.1]
[mysql-server] [5.0.45]
[p5-Archive-Tar] [1.30]
[php5-gd] [5.2.3] [no_x11]



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
donebrowsers
Guest
Posts: n/a
 
      03-12-2008
While that works and I appreciate it, I was just using the []s as a
placeholder. I'm actually using PHP's <a href="http://us3.php.net/
manual/en/function.preg-match.php">preg_match()</a> function which
uses PERL style regular expressions. I submitted it to this group
because PERL programmers tend to be better with regular expressions
than anyone else.

This function essentially matches parts and adds them to an array with
[0] matching the whole string, [1]... matching the ()s. So what I
really have is:
array(3) {
[0]=>
string(12) "zoo-2.10.1p1"
[1]=>
string(3) "zoo"
[2]=>
string( "2.10.1p1"
}
array(3) {
[0]=>
string(23) "mutt-1.4.2.3-compressed"
[1]=>
string(12) "mutt-1.4.2.3"
[2]=>
string(10) "compressed"
}
array(3) {
[0]=>
string(22) "lha-1.14i.ac20050924.1"
[1]=>
string(3) "lha"
[2]=>
string(1 "1.14i.ac20050924.1"
}
array(3) {
[0]=>
string(19) "mysql-server-5.0.45"
[1]=>
string(12) "mysql-server"
[2]=>
string(6) "5.0.45"
}
array(3) {
[0]=>
string(19) "p5-Archive-Tar-1.30"
[1]=>
string(14) "p5-Archive-Tar"
[2]=>
string(4) "1.30"
}
array(3) {
[0]=>
string(20) "php5-gd-5.2.3-no_x11"
[1]=>
string(13) "php5-gd-5.2.3"
[2]=>
string(6) "no_x11"
}

What I want is ie:
array(4) {
[0]=>
string(20) "php5-gd-5.2.3-no_x11"
[1]=>
string(13) "php5-gd"
[2]=>
"5.2.3"
[3]=>
string(6) "no_x11"
}

Thanks for your suggestion though, sorry for the confusion.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-12-2008
>>>>> "d" == donebrowsers <(E-Mail Removed)> writes:

d> While that works and I appreciate it, I was just using the []s as a
d> placeholder. I'm actually using PHP's <a href="http://us3.php.net/
d> manual/en/function.preg-match.php">preg_match()</a> function which
d> uses PERL style regular expressions. I submitted it to this group
d> because PERL programmers tend to be better with regular expressions
d> than anyone else.

it is Perl, never PERL. preg is NOT perl, nor is it compatible with
perl. that is why we use Perl and not php. the answer you got was valid
perl and that will likely be all you will get here.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
donebrowsers
Guest
Posts: n/a
 
      03-12-2008
Fine. How would I use perl to do what I am trying to do? Strip out
those parts and add them to an array?
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-12-2008
>>>>> "d" == donebrowsers <(E-Mail Removed)> writes:

d> Fine. How would I use perl to do what I am trying to do? Strip out
d> those parts and add them to an array?

what was wrong with the answer you got? it split the package names into
the parts you wanted. grabbing those and making them into arrays is
trivial. just assign the grabs to an array or put the split into [] to
make an anon array. then you can build up the data structure from that.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-12-2008
On Wed, 12 Mar 2008 12:31:20 -0700, donebrowsers wrote:

> I have the following dataset:
> zoo-2.10.1p1
> mutt-1.4.2.3-compressed
> lha-1.14i.ac20050924.1
> mysql-server-5.0.45
> p5-Archive-Tar-1.30
> php5-gd-5.2.3-no_x11
>
> There are package listings on an OpenBSD machine. I want to parse the
> package name, the version, and if there is a flavor, that too. I want
> the following:
> [zoo] [2.10.1p1]
> [mutt] [1.4.2.3] [compressed]
> [lha] [1.14i.ac20050924.1]
> [mysql-server] [5.0.45]
> [p5-Archive-Tar] [1.30]
> [php5-gd] [5.2.3] [no_x11]
>
> I currently have this regex (which is close but doesn't quite work):
> /(^.*)-(.*)($-\w)?/
>
> It currently gives me:
> [zoo] [2.10.1p1]
> [mutt-1.4.2.3] [compressed]
> [lha] [1.14i.ac20050924.1]
> [mysql-server] [5.0.45]
> [p5-Archive-Tar] [1.30]
> [php5-gd-5.2.3] [no_x11]
>
> As you can see I'm having issues with the flavor (the last part of the
> package name which not every package has) part. Any help?


How do you determine what part is what in p5-Archive-Tar-1.30? The easy
solution (non-greedy regexpen, look it up in perldoc perlre) will
misparse this entry, it will give [p5-Archive] [Tar] [1.30].

Assuming the version always starts with a digit, and never has a dash,
you may want to try (untested):
/^(.*?)-(\d[^-]*)(?.*))?$/

HTH,
M4
 
Reply With Quote
 
donebrowsers
Guest
Posts: n/a
 
      03-12-2008
That's it! Thanks Martijn.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-12-2008
donebrowsers <(E-Mail Removed)> wrote:
>I have the following dataset:
>zoo-2.10.1p1
>mutt-1.4.2.3-compressed
>lha-1.14i.ac20050924.1
>mysql-server-5.0.45
>p5-Archive-Tar-1.30
>php5-gd-5.2.3-no_x11
>
>There are package listings on an OpenBSD machine. I want to parse the
>package name, the version, and if there is a flavor, that too. I want
>the following:
>[zoo] [2.10.1p1]
>[mutt] [1.4.2.3] [compressed]
>[lha] [1.14i.ac20050924.1]
>[mysql-server] [5.0.45]
>[p5-Archive-Tar] [1.30]
>[php5-gd] [5.2.3] [no_x11]


The examples are nice, but an additional verbal description of what you are
trying to do would help a lot.
Are you trying to split the string at each dash (minus sign) and store the
pieces in an array?

That is trivial:
@pieces = split /-/, $string;

jue
 
Reply With Quote
 
donebrowsers
Guest
Posts: n/a
 
      03-12-2008
Yes and no. There are -s in the name of the package, for example php5-
core. I want the package name php5-core, and the version, 5.2.3; and
if there is a flavor, for example the mutt package, I want that
separate. Martijn's regex works perfectly.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments