Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular expression.

Posted on 2001-06-21
14
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +5
14 Comments
 
LVL 84

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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 84

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

636 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