Solved

Need help with regex?

Posted on 2002-04-30
17
122 Views
Last Modified: 2010-03-05
how do i search for a string that -
"/net/vol01/shot/curtest.../2fa.ca"

and I need to print everything from "/shot... " onwords...
so that
$FILE = /shot/cuetest/wrappers/pix/out/wrappers-2/2fa

Any suggestions....

Thanks
0
Comment
Question by:sdesar
17 Comments
 
LVL 1

Expert Comment

by:bluprint
ID: 6981881
assuming:
$FILE = "/net/vol01/shot/curtest.../2fa.ca"


then:
$FILE =~ s{/net/vol01(.*)}{$1};

or

$FILE =~ s{(/shot/.*)}{$1};

##############################################

or assuming:
$MY_VAR = "/net/vol01/shot/curtest.../2fa.ca"

then:
$MY_VAR =~ m{/net/vol01(.*)};
$FILE = $1;

or

$MY_VAR =~ m{(/shot/.*)};
$FILE = $1;

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6982083
s#/net/vol01(.*)#$1#;
# or
s#(/[^/]*){1,2}(.*)#$2#;
0
 

Author Comment

by:sdesar
ID: 6991499
ed help with one more regex-


my $pwd = $ENV{PWD};


$pwd = /shot/cuetest/wrappers/pix/out/wrappers_test_v3/2fa


I need to ge the word 'test'  its always gain to be between

the two underscores ie  '_'.


Thanks
0
 

Author Comment

by:sdesar
ID: 6991505
BTW ..the word 'test' is between '_' and '_v'

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6991735
s#.*_(test)_v.*#$1#;
0
 

Author Comment

by:sdesar
ID: 6991783
but, the word 'test will keep changing evertime the user types pwd on a different machine so
I need the ability to recognize the word between a '_' and '_v'

Awaiting more suggestions..

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6991876
.. what's wrong with my suggestion?
0
 

Author Comment

by:sdesar
ID: 6991885
the word test is in brackets... doesn't that mean it looks for test?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sdesar
ID: 6991912
#!/usr/local/bin/perl
      2
      3 use strict;
      4
      5  my $pwd = $ENV{PWD};
      6
      7  print "file is : $pwd \n";

         # pwd is - /shot/cuetest/wrappers/pix/out/wrappers_test_v3/2fa.cin
      8
      9
     10
     11 my $take =~ s#.*_(test)_v.#$1#;
     12
     13  while ($pwd = <>) {
     14
     15    if ($pwd = ~/$take/o) {  print "Take : $pwd \n"; }
     16
     17  }
     18
     19

Is this the right way to do it?
0
 

Author Comment

by:sdesar
ID: 6991916
Seems like 'test' is hard coded.. but its going to change on different machines.
the word test is in brackets... doesn't that mean it looks for test?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 6991918
s#.*_([^_]*)_v.*#$1#;
0
 

Author Comment

by:sdesar
ID: 6992301

#!/usr/local/bin/perl
      2
      3 use strict;
      4
      5  my $pwd = $ENV{PWD};
      6
      7  print "file is : $pwd \n";

         # pwd is - /shot/cuetest/wrappers/pix/out/wrappers_test_v3/2fa.cin
      8
        my $newtake;
      9
     10
     11 my $take =~ s#.*_([^_]*)_v.*#$1#;
     12
     13  while ($pwd = <>) {
     14
     15    if ($pwd = ~/$take/o) {  print "Take : $pwd \n"; }
     16
     17  }
     18
         print "Take : $newtake \n";
     19
     
     24
     25  exit;

I need to be able to get the file name from $pwd
get the take from $take =~ s#.*_([^_]*)_v.*#$1#;

And store the result in $newtake.

Here's the code that I currently have.  But, its just printing Take: 4294898770  .. some wierd numbers rather than printing 'test'.


Thanks for all your help.

Awaiting suggestions
0
 
LVL 84

Expert Comment

by:ozo
ID: 6992478
$pwd = ~/$take/o
should be
$pwd =~ /$take/o
0
 

Author Comment

by:sdesar
ID: 6992493
Thanks.. I modified it to $pwd =~ /$take/o  , but now it prints no data ie Take :

more suggestions
0
 

Author Comment

by:sdesar
ID: 6992582
Thanks ozo and  ahoffmann .. I figured out my probelm.

Fixed it!

It works great!
0
 

Author Comment

by:sdesar
ID: 6992583
Thanks.

here are your points!
0
 

Author Comment

by:sdesar
ID: 6999206
One more regex I need help with..

Here's the file -
wrapper_cmp_v2_2kfa_lg8.1-10#.cla

I need to look for '1-10'
The number 1-10 will vary but its always going to be after the . and have a # after it.

Any suggestions.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

20 Experts available now in Live!

Get 1:1 Help Now