Solved

Regular expression.

Posted on 2001-06-21
14
240 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

856 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