LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-20-2006, 01:09 PM   #1
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Rep: Reputation: 15
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.
 
Old 05-20-2006, 01:30 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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: [^/]
 
Old 05-20-2006, 02:09 PM   #3
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
It works, thanks.
 
Old 05-20-2006, 03:32 PM   #4
xemous
Member
 
Registered: Jun 2004
Location: Australia, Melbourne.
Distribution: Ubuntu
Posts: 80

Original Poster
Rep: Reputation: 15
Is there anyway to count /'s in a string and store them in a scalar?
 
Old 05-20-2006, 03:45 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
At least you could use something like
Code:
$string =~ s/[^\/]//g;
print length( $string );
 
Old 05-20-2006, 04:11 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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;
 
Old 05-22-2006, 10:09 AM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Or:
Code:
        my $cnt = $mystring =~ tr|/||;
        print $cnt , "\n";
tr can be non-destructive.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl regular expressions CyberJedi Programming 2 10-02-2005 11:17 AM
Perl regular expressions daYz Programming 6 08-11-2005 07:27 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 11:32 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 05:16 PM


All times are GMT -5. The time now is 05:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration