Solved

Regular expression.

Posted on 2001-06-21
14
234 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 84

Accepted Solution

by:
ozo earned 100 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing shell scripts to run on the SFTP cmd object 12 182
Perl - Mawk 2 96
Using Perl to parse rows 7 95
Perl regex to replace any capital letters not preceded by ">" 6 119
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 …
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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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