Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: Problem in pattern matching with alternation

by perladdict (Chaplain)
on Aug 12, 2007 at 17:35 UTC ( #632085=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Problem in pattern matching with alternation
in thread Problem in pattern matching with alternation

Hi graff,
Here i am explaining in detail what actually i am trying to extract.
I am working on a task to get the vob storage path from the list of vobtags.below is the command i am running on command prompt
bash-3.00$cleartool lsvob -s #which will give all vobtag list as below.

/vobs/cs_test_scripts /vobs/openwall_pam_userpass_rel /vobs/cs_cim /vobs/PIP_PVOB /vobs/pvob_ic /v_dialer3rdparty /v_dialerclient /v_dialer_rel /scm
#!/usr/bin/perl -w $arg=$ARGV[0]; $cmd="cleartool lsvob $arg" $arr=`$cmd`; print "$arr\n";
after running this script with argument as vobtag list that i got from running "cleartool lsvob -s" command
like as bash-3.0$cleartool lsvob /vobs/cs_test_scripts from command prompt will give the result as below
* /vobs/cs_test_scripts /usr/add-on/puccase_vob01/ccvob01/cs_test_scri +pts.vbs public (replicated) * /scm /usr/add-on/puccase_vob01/ccvob01/scm.test.vbs
the output contains three parts */vobs/cs_test_script,*/scm are as vobtags
/usr/add-on/puccase_vob01/ccvob01/cs_test_scripts.vbs is vobstorage directory path
and last part is type as "public (replicated)".
from this i want to extract vobstorage path of each vobtag that i am supling vobtag(/vobs/cs_test_script)as argument to the below script,in that i am matching the output for the tags with 2 slashes in it ex:/vobs/cs_test_script and
vobtag with one slash in it ex:/scm
with regular expression that has to match the output for both type of vobtags(/vobs/cs_test_script,/scm)by using alternation operator in patternmatching as below script
#!/usr/bin/perl $arg=$ARGV[0]; $cmd1="cleartool lsvob $arg"; $arr=`$cmd1`; print "$arr\n"; $storage1=$1 if($arr=~/^*\s+\/\w+\/\w+\s+(.+)\s+\w+/); print "$storage1\n"; $storage2=$1 if($arr=~/^*\s+\/\w+\s+(.+)\s+\w+/); print "$storage2\n"; $storage3=$1 if($arr=~/^*\s+\/\w+\/\w+\s+(.+)\s+\w+|^*\s+\/\w+\s+(.+)\ +s+\w+/); print "$storage3\n";
I am running the script with both type of vobtags as /vobs/cs_test_scripts and /scm as arguments so its printing the match for both $storage1 and $storage2 as below
bash-3.00$ perl scriptname.pl /vobs/cs_test_script
bash-3.00$ perl scriptname.pl /scm
* /vobs/cs_test_scripts /usr/add-on/puccase_vob01/ccvob01/cs_test_scri +pts.vbs public (replicated) #match storage1 /usr/add-on/puccase_vob01/ccvob01/cs_test_scripts.vbs * /scm /usr/add-on/puccase_vob01/ccvob01/scm.vbs publi +c (replicated) #storage2 /usr/add-on/puccase_vob01/ccvob01/scm.vbs
In $storage3 variable i am combining the pattern match of storage1 and storage2 by alternation operator,it should work for both type of vobtag arguments,i am not getting where i am doing wrong...
Monks sorry for repeated posting of same qustion again and again...the problems seems to simple for u people ....


Comment on Re^4: Problem in pattern matching with alternation
Select or Download Code
Re^5: Problem in pattern matching with alternation
by FunkyMonk (Canon) on Aug 12, 2007 at 17:57 UTC
    That's better (I think). Your input data look like this:

    * /vobs/cs_test_scripts /usr/add-on/puccase_vob01/ccvob01/cs_test_scri +pts.vbs public (replicated) * /scm /usr/add-on/puccase_vob01/ccvob01/scm.test.vbs

    And your output should be:

    /usr/add-on/puccase_vob01/ccvob01/cs_test_scripts.vbs /usr/add-on/puccase_vob01/ccvob01/scm.test.vbs

    I wouldn't do this with regular expressions, I'd use split:

    my @data = ( "* /vobs/cs_test_scripts /usr/add-on/puccase_vob01/ccvob01/cs_test_s +cripts.vbs public (replicated)", "* /scm /usr/add-on/puccase_vob01/ccvob01/scm.test.vbs" ); for ( @data ) { my $path = (split)[2]; print "$path\n"; }
    Outputs:

    /usr/add-on/puccase_vob01/ccvob01/cs_test_scripts.vbs /usr/add-on/puccase_vob01/ccvob01/scm.test.vbs
    Is that what you're after?
Re^5: Problem in pattern matching with alternation
by graff (Chancellor) on Aug 12, 2007 at 20:13 UTC
    Here i am explaining in detail what actually i am trying to extract.

    And you seem to be completely ignoring (or not understanding) the code sample and suggestions that I posted. If you don't understand something I've said or something in my code, it's okay to quote me in a reply and say that you don't understand.

    i am matching the output for the tags with 2 slashes in it ex:/vobs/cs_test_script and vobtag with one slash in it ex:/scm with regular expression that has to match the output for both type of vobtags(/vobs/cs_test_script,/scm)by using alternation operator in pattern matching as below script

    Checking your code, I have learned something. I expected that if a regex that starts like this:  /^*\s+/ it would be a syntax error, and the script would not run at all. But having tried it, I see that it does run (it's not an error), and it even seems to work: $_="* foo"; m{^*\s+foo} returns true.

    Still, I prefer using a backslash when I want to match a literal "*" character. Note that  m{*\s+foo} is a syntax error.

    (And make sure you understand the distinction: slash is "/", backslash is "\", and the two have very different meanings and uses in perl.)

    i am combining the pattern match of storage1 and storage2 by alternation operator

    You don't need to use an alternation (|) in the regex. The code that I suggested above uses a quantifier, so that one, two, three or more slashes in the path string can be treated by the single expression -- shown here with commentary:

    m{^ # at the start of the string \* # match a literal asterisk character \s+ # then one or more whitespace characters (?: # begin a non-capturing group expression / # match a literal slash character \w+ # then one or more alphanumeric_word characters )+ # close the group, match 1 or more instances of that expr +ession \s+ # then one or more whitespace characters (\S+) # capture a group of non-whitespace characters }x # end of regex (x modifier lets comments and spacing be i +gnored)

    The last couple things that you are not paying attention to are: whether you need to be processing your data line by line, rather than slurping all the lines into a single "$arr" variable, and whether you would be better off using "split" instead of regex matches.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://632085]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-07-06 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls