Solved

Regular expression.

Posted on 2001-06-21
14
226 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now