Connecting Tech Pros Worldwide Forums | Help | Site Map

regular expression

mehj123's Avatar
Member
 
Join Date: Aug 2007
Posts: 55
#1: Oct 16 '07
HI...

I want to retrieve the view name from an expression like this
Expand|Select|Wrap|Line Numbers
  1.  $element = "/Net/xyz/abc/viewstore/hello.vws";
  2.  
i.e i want to retrieve hello.... the problem is that these slashes can sometimes be like
Expand|Select|Wrap|Line Numbers
  1.  $element = "\Net\xyz\abc\viewstore\hello.vws";
  2.  
depending upon if it is windows or unix...can anyone plz help me?
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Oct 16 '07

re: regular expression


what have you tried so far?
mehj123's Avatar
Member
 
Join Date: Aug 2007
Posts: 55
#3: Oct 16 '07

re: regular expression


Quote:

Originally Posted by KevinADC

what have you tried so far?

This is what I have tried.. But this works only for "/" slashes

Expand|Select|Wrap|Line Numbers
  1.  
  2. if($element =~ m/\//)
  3. {
  4. @el = split(/\//,$element);
  5. $c = @el-1;
  6. ($view = $el[$c]) =~ s/\.(.*)//g;
  7. print $view;
  8. }
  9.  
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,569
#4: Oct 16 '07

re: regular expression


Quote:

Originally Posted by mehj123

This is what I have tried.. But this works only for "/" slashes

Expand|Select|Wrap|Line Numbers
  1.  
  2. if($element =~ m/\//)
  3. {
  4. @el = split(/\//,$element);
  5. $c = @el-1;
  6. ($view = $el[$c]) =~ s/\.(.*)//g;
  7. print $view;
  8. }
  9.  

Well, I have hard coded $element in my example below. Just uncomment the one you want to try, they both work. You can examine the split page on perldoc and see how it works. Now, all you have to do is grab the element you need from the array.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. #my $element = "/Net/xyz/abc/viewstore/hello.vws";
  5. my $element = "\\Net\\xyz\\abc\\viewstore\\hello.vws";
  6.  
  7. my @segments = split(/[\/\\]/, $element);
  8.  
  9. print("@segments\n");
  10.  
Regards,

Jeff
mehj123's Avatar
Member
 
Join Date: Aug 2007
Posts: 55
#5: Oct 17 '07

re: regular expression


Quote:

Originally Posted by numberwhun

Well, I have hard coded $element in my example below. Just uncomment the one you want to try, they both work. You can examine the split page on perldoc and see how it works. Now, all you have to do is grab the element you need from the array.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. #my $element = "/Net/xyz/abc/viewstore/hello.vws";
  5. my $element = "\\Net\\xyz\\abc\\viewstore\\hello.vws";
  6.  
  7. my @segments = split(/[\/\\]/, $element);
  8.  
  9. print("@segments\n");
  10.  
Regards,

Jeff

Thanks... But the value of $element is read from a file which has the lines like \Net\xyz\abc\viewstore\hello.vws .. soo is it possible to stuff the escape characters to each line in the file?
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Oct 17 '07

re: regular expression


Quote:

Originally Posted by mehj123

Thanks... But the value of $element is read from a file which has the lines like \Net\xyz\abc\viewstore\hello.vws .. soo is it possible to stuff the escape characters to each line in the file?


You don't need to escape a backslash that is input from a file. The code Jeff posted should work to split a string on forward slashes or back slashes.
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#7: Oct 17 '07

re: regular expression


Have you tried to incorporate Jeff's example into your code. It will work no matter where the $element value is coming from.
mehj123's Avatar
Member
 
Join Date: Aug 2007
Posts: 55
#8: Oct 17 '07

re: regular expression


Thanks a lot Jeff.. It worked.
Reply