[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression.

Posted on 2001-06-21
14
Medium Priority
?
262 Views
Last Modified: 2012-05-04
Hello.....
I have some strings......
FILE.This.a.name.jpg.log
FILE.name.jpg.log
FILE.jpg.log

I need a regular expression to turn the above into....
FILE.This.a.name
FILE.name
FILE

ie: I want to end up with the two extension being chopped.
no matter how many stops are in the rest of the string.

I have another scenario where i want to remove everything upto the first stop, keep everything upto the second from last fullstop.

so this would mean (for the above strings)
I end up with.....

This.a.name
FILE.name
null

thanks in advance for your kind help
regards
0
Comment
Question by:rickyr
  • 3
  • 2
  • 2
  • +5
14 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 6216237
for( @strings=qw(FILE.This.a.name.jpg.log FILE.name.jpg.log FILE.jpg.log) ){
    s/\.[^.]*\.[^.]*$//;
    print "$_\n";
}
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6216519
maybe use + not *
0
 
LVL 3

Author Comment

by:rickyr
ID: 6217913
Sorry ozo, I realised that I need to put everything upto the first stop into one var and everything after that upto the second from last stop......

FILE.This . is . my . value.jpg.log

$var1 should contatin "FILE"
$var2 should contain "This . is . my . value"

Thanks
....R
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

Accepted Solution

by:
ozo earned 400 total points
ID: 6217954
$_ = "FILE.This . is . my . value.jpg.log"
if( ($var1,$var2) = /^([^.]*)\.(.*)\..*\./ ){
     print "$var1\n$var2\n";
}
0
 
LVL 1

Expert Comment

by:mrmicky
ID: 6219061
There's a reason why we have functions for arrays and lists built into Perl.  Regular expressions are great but not always the best solution.  Try the following example:

#!/usr/bin/perl

use strict;

my @strings=('FILE.This.a.name.jpg.log',
             'FILE.name.jpg.log',
             'FILE.jpg.log');

foreach(@strings){
  my @string=split(/\./);
  for(1..2){          #remove the last element twice
    my $junk=pop @string;
  }
  print "First scenario: ",join('.',@string),"\n";

  my $junk=shift @string;
  print "Second scenario: ",join('.',@string),"\n\n";
}



0
 
LVL 1

Expert Comment

by:anant99
ID: 6223816
This should do it :-

$whatever =~ s/\.jpg\.log//g;

Anant
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6225730
that's what I'd use if I was sure of jpg log too.
0
 
LVL 1

Expert Comment

by:anant99
ID: 6227034
hey rickyr,

is it always going to have the same extension ?
will the order always be the same ?

$whatever = "FILE.Thisname.jpg.log";
$whatever =~ s/(\S+)\.(\S+)\.(\S+)\.(\S+)/$2/g;

but there should be not dots in the "Thisname"

I think this should do it. It removes the File , image ext and log.
0
 
LVL 1

Expert Comment

by:mehdi
ID: 6228321
I havent touche regexps in a ehile.. but id say..

chop off extension -

$var = "FILE.This.a.name.jpg.log";
print "var is.. " . $var;

$var =~ s/\.\w+\.\w+$//i;
print "var is now.. " . $var;

Your second scenario.. is your SECOND example not wrong?

FILE.name should be "name" because everything upto the first  . should be chopped ?

~~~~~~~~~~~~~~~~~~~~~~~~~~~
The secret lemonade drinker

0
 
LVL 12

Expert Comment

by:Amick
ID: 6245645
Try this and see if it isn't the most general purpose solution to your question:

#I have some strings......
push(@names,"FILE.This.a.name.jpg.log");
push(@names,"FILE.name.jpg.log");
push(@names,"FILE.jpg.log");
#I need a regular expression to turn the above into....
#FILE.This.a.name
#FILE.name
#FILE

foreach $a (@names)
{
     $a=~ s/(\.[^\.]*\.[^\.]*$)//;
     print $a , "\n";
}

0
 
LVL 12

Expert Comment

by:Amick
ID: 6245684
Please disregard my proposed answer.  I didn't see ozo's posting and your response until too late.
0
 
LVL 3

Expert Comment

by:modder
ID: 6250347
Hi Amick,

I rejected the answer as requested. Thanks.

modder
Community Support Moderator@Experts-Exchange
0
 
LVL 3

Author Comment

by:rickyr
ID: 6250642
Thanks everyone
...R
0
 
LVL 3

Author Comment

by:rickyr
ID: 6255761
Thanks ozo, can u just show me how to grap everything beyond the last period?????
ta
...R
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question