INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

"Case of the Mondays" Regexp Problem

"Case of the Mondays" Regexp Problem

(OP)
It's Monday and I'm sure I am doing something stupid with my regexp.  But because it's Monday, I am looking for someone to tell me what I am doing wrong.

In essence, I am trying to parse out a Linux component string from its version string.  Because the component name can contain one or more digits, I see

CODE

[jdoe:cheech ~]$ tclsh
% set rpm foo123-0.7.8-9
foo123-0.7.8-9
% regexp {(.*?)-(\d.*)} $rpm match rpmName rpmVers
1
% puts $rpmName
foo123
% puts $rpmVers
0
% puts $match
foo123-0
%

RE: "Case of the Mondays" Regexp Problem

(OP)
Sorry. I submitted perhaps to early. If you notice above, I am only able to capture '0' from '0.7.8-9'

RE: "Case of the Mondays" Regexp Problem

Hi

And what is your goal ?

If you want to get "foo123" in $rpmName and "0.7.8-9" in $rpmVers, I would change the expression to :

CODE --> Tcl

regexp {(.*?)-(.*)$} $rpm match rpmName rpmVers

Feherke.
http://feherke.github.com/

RE: "Case of the Mondays" Regexp Problem

CODE

set rpm "foo123-0.7.8-9" if {[regexp {(.*)\-(\d+\..*)} $rpm match rpmName rpmVers]} { puts "\$rpmName = '$rpmName'" puts "\$rpmVers = '$rpmVers'" puts "\$match = '$match'" }
Output:

CODE

$rpmName = 'foo123' $rpmVers = '0.7.8-9' $match = 'foo123-0.7.8-9'

RE: "Case of the Mondays" Regexp Problem

Hi

Just a minor note.

I am not familiar with the RPM package naming schemes, I use Frugalware and there the packages keep the original name and version as far as possible. So there are packages called erlang-15B01-1, fontforge-20110222-1, j2re-6-31 for which mikrom's regular expression would fail. There are also packages called alsa-firmware-1.0.25-1, c-ares-1.7.5-1, gnome-desktop-3.4.2-1 for which my regular expression would fail. Of course there are also packages like ca-certificates-20090814-6, systemd-sysvinit-44-1, terminus-font-ttf-33-3 for which both regular expressions would fail.

So I would try to ask rpm to extract and provide those information, then parse its output :

CODE --> Tcl

set rpm "erlang-R15B-01.2.fc18.x86_64.rpm" set data [exec rpm -qip $rpm] foreach one [split $data "\n"] { if {[lindex $one 0]=="Name"} { set rpmName [lindex $one 2] } if {[lindex $one 0]=="Version"} { set rpmVers [lindex $one 2] } } puts "\$rpmName = '$rpmName'" puts "\$rpmVers = '$rpmVers'"

CODE --> output

$rpmName = 'erlang' $rpmVers = 'R15B'
In this example I used a local file as there is no RPM database on my machine. To make it query data from your database seems you have to only remove the -p switch.

Feherke.
http://feherke.github.com/

RE: "Case of the Mondays" Regexp Problem

(OP)
Thanks for all the replies. Feherke, your last post is the best approach since it is fool proof. Thank you very much.

I wasn't very clear in my post and I apologize. I am not a Linux packaging guy whatsover but I noticed on the product I'm working on is that there are multiple different patterns in the RPM names. And to determine the name vs. the version is not trivial (since the name may contain digits, underscores, and hyphens). The only pattern which I could manually recognize was the hyphen followed by a digit (which is why my original regexp had {-\d}).

However, I guess I would still be curious as to why .7.8-9 is lost below. Although I will use the 'rpm -qip', it still will bother me as to why the following regexp does not work as expected for foo123-0.7.8-9.

regexp {(.*?)-(\d.*)} foo123-0.7.8-9 match rpmName rpmVers

RE: "Case of the Mondays" Regexp Problem

Hi

Quote (Feherke)

it still will bother me as to why the following regexp does not work as expected for foo123-0.7.8-9.
Sorry, I have no proper answer for this. There is a sentence in re_syntax which rises curiosity :

Quote (re_syntax)

The matching rules for REs containing both normal and non-greedy quantifiers have changed since early beta-test versions of this package.
But I found no details on this.

Actually Tcl uses ARE ( Advanced Regular Expressions ) not the usual PCRE ( Perl Compatible Regular Expressions ), so maybe our expectation is wrong. But while that ? clearly influences the greediness, I would expect identical behavior.

My test indicates that if the first quantifier of the regular expression is non-greedy, all quantifiers become non-greedy. Making other quantifier then the first one non-greedy is without effect. Strange.

CODE --> Tcl

# command $g1 $g2 $g3 $g4 regexp {(.*)-(.*)-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz baz regexp {(.*)-(.*)-(.*)-(.*?)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz baz regexp {(.*?)-(.*)-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz regexp {%*(.*)-(.*)-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz baz regexp {%*?(.*)-(.*)-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz regexp {(.*)-(.*)%*-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz baz regexp {(.*)-(.*)%*?-(.*)-(.*)} foo-bar-fiz-baz m g1 g2 g3 g4 # foo bar fiz baz

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close