Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-20-2006, 01:09 PM
|
#1
|
|
Member
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80
Rep:
|
Regular Expressions, Perl, everything but a /
Hi, got a question about regular expressions in perl.
I need to count how many slashes are in a string, so...
Code:
$path = /hi/you/crazy/.guy.**g/lol
I'll use this...
Code:
if ($path =~ m%^/.+/.+/.+/.+/.+$%) {
print 'whoa!, that directory is five levels deep, It works! (not really, help)
}
I can't find a simple to way to say ``any set of characters UNLESS it's a slash'', as the (.) counts everything, including slashes, and the /w doesn't account for other characters, ie: (*), and (.).
Last edited by xemous; 05-20-2006 at 01:12 PM.
|
|
|
|
05-20-2006, 01:30 PM
|
#2
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
The ^(/).(/)?.(/)?.(/)?.$ construct will put / in $1, $2, $3 ... (if there is one) and so on. You have to start and end the regex with somecharacter other than a / -- eg, try #
This matches everything but a slash: [^/]
|
|
|
|
05-20-2006, 02:09 PM
|
#3
|
|
Member
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80
Original Poster
Rep:
|
It works, thanks.
|
|
|
|
05-20-2006, 03:32 PM
|
#4
|
|
Member
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80
Original Poster
Rep:
|
Is there anyway to count /'s in a string and store them in a scalar?
|
|
|
|
05-20-2006, 03:45 PM
|
#5
|
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
At least you could use something like
Code:
$string =~ s/[^\/]//g;
print length( $string );
|
|
|
|
05-20-2006, 04:11 PM
|
#6
|
|
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 13.37
Posts: 4,084
Rep: 
|
If all you want is the count and not to modify the original string, the following works:
Code:
$count = 0;
$count++ while $string =~ m|/|g;
|
|
|
|
05-22-2006, 10:09 AM
|
#7
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
Or:
Code:
my $cnt = $mystring =~ tr|/||;
print $cnt , "\n";
tr can be non-destructive.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:24 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|