For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2005 > split regex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author split regex
Umesh T G

2005-12-15, 7:55 am

Hi List,

I want to get that value of *-i* set in below line.

*Options=-a"hello" -mt -ml3 -i"dir1\dir2" -k -p -i"dir3\dir4" -m*

What is the best way to get the value of *-i* values into one array. For
ex: I want the output as: *dir1\dir2* and *dir3\dir4* into one array.

Thanks in advance.

Thanks
Umesh

John Doe

2005-12-15, 7:55 am

Umesh T G am Donnerstag, 15. Dezember 2005 13.00:
> Hi List,


Hi Umesh

> I want to get that value of *-i* set in below line.
>
> *Options=-a"hello" -mt -ml3 -i"dir1\dir2" -k -p -i"dir3\dir4" -m*
>
> What is the best way to get the value of *-i* values into one array. For
> ex: I want the output as: *dir1\dir2* and *dir3\dir4* into one array.


What possibilities did you try (show us some code), from which the best
could be choosed, or to which a better could be added?

joe
Umesh T G

2005-12-15, 7:55 am

Hi John,

I tried like this, where $line='*Options=-a"hello" -mt -ml3 -i"dir1\dir2"
-k -p -i"dir3\dir4" -m'*



if (grep/^Options/,$line)
{

@inc=split(/-i/,$line);

foreach $word (@inc)
{
print "$word\n";
}



I do not know how to proceed after this.....


Thanks for your quick reply...



Thanks
Umesh




On 12/15/05, John Doe <security.department@tele2.ch> wrote:
>
> Umesh T G am Donnerstag, 15. Dezember 2005 13.00:
>
> Hi Umesh
>
> array. For
> array.
>
> What possibilities did you try (show us some code), from which the best
> could be choosed, or to which a better could be added?
>
> joe
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


John Doe

2005-12-15, 6:57 pm

Umesh T G am Donnerstag, 15. Dezember 2005 14.20:
> Hi John,


Hi Umesh

> I tried like this, where $line='*Options=-a"hello" -mt -ml3 -i"dir1\dir2"
> -k -p -i"dir3\dir4" -m'*
>
>
>
> if (grep/^Options/,$line)
> {
>
> @inc=split(/-i/,$line);
>
> foreach $word (@inc)
> {
> print "$word\n";
> }
>
>
>
> I do not know how to proceed after this.....
> Thanks for your quick reply...


You're welcome...

try the following:

a) put
use strict;
use warnings;
at the beginning of the script

b) look at the warnings and then declare the vars with my, see
perldoc -f my

c)
read the documentation
perldoc -f grep
to see why grep is not appropriate in the if clause

d) check the documentation
perldoc -f perlre
and the others mentioned at the bottom to see why
/^Options/ won't ever match a string beginning with '*' and why your script
doesn't print anything

e) hint:
don't use if, only use a regex to extract the parts you like, and extract them
directly into an array by catching the parts you like

.... and post the new code then :-)

greetings joe


[color=darkred]
>
>
>
> Thanks
> Umesh
>
> On 12/15/05, John Doe <security.department@tele2.ch> wrote:
Umesh T G

2005-12-15, 6:57 pm

Hi Joe,

just to correct my prevoius one, my $line does not have * in the begining,
that was a type. the $line value is like this

*Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p -i"dir3\dir4" -m*



my grep return the values correct to my array and the values in array will
be like this obviously:

*Options=-a"hello" -mt -ml3 *
*"dir1\dir2"-k -p *
*"dir3\dir4" -m*
**
**
But, what I'm interested in getting the output values are:
*"dir1\dir2"*
*"dir3\dir4"*

I do not know how to get them......can u suggest pls.?


thanks
Umesh
**

**
**






On 12/15/05, John Doe <security.department@tele2.ch> wrote:
>
> Umesh T G am Donnerstag, 15. Dezember 2005 14.20:
>
> Hi Umesh
>
> -i"dir1\dir2"
>
> You're welcome...
>
> try the following:
>
> a) put
> use strict;
> use warnings;
> at the beginning of the script
>
> b) look at the warnings and then declare the vars with my, see
> perldoc -f my
>
> c)
> read the documentation
> perldoc -f grep
> to see why grep is not appropriate in the if clause
>
> d) check the documentation
> perldoc -f perlre
> and the others mentioned at the bottom to see why
> /^Options/ won't ever match a string beginning with '*' and why your
> script
> doesn't print anything
>
> e) hint:
> don't use if, only use a regex to extract the parts you like, and extract
> them
> directly into an array by catching the parts you like
>
> ... and post the new code then :-)
>
> greetings joe
>
>
>
> best
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Shawn Corey

2005-12-15, 6:57 pm

Umesh T G wrote:
> I tried like this, where $line='*Options=-a"hello" -mt -ml3 -i"dir1\dir2"
> -k -p -i"dir3\dir4" -m'*



This line won't compile. Which of the following is it?

$line='Options=-a"hello" -mt -ml3 -i"dir1\dir2" -k -p -i"dir3\dir4" -m';

$line='*Options=-a"hello" -mt -ml3 -i"dir1\dir2" -k -p -i"dir3\dir4" -m*';

Please test your code before posting.


--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
Shawn Corey

2005-12-15, 6:57 pm

Umesh T G wrote:
> Hi Joe,
>
> just to correct my prevoius one, my $line does not have * in the begining,
> that was a type. the $line value is like this
>
> *Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p -i"dir3\dir4" -m*
>
>
>
> my grep return the values correct to my array and the values in array will
> be like this obviously:
>
> *Options=-a"hello" -mt -ml3 *
> *"dir1\dir2"-k -p *
> *"dir3\dir4" -m*
> **
> **
> But, what I'm interested in getting the output values are:
> *"dir1\dir2"*
> *"dir3\dir4"*
>
> I do not know how to get them......can u suggest pls.?
>
>
> thanks
> Umesh


#!/usr/bin/perl

use strict;
use warnings;

my $line='Options=-a"hello" -mt -ml3 -i"dir1\dir2" -k -p -i"dir3\dir4" -m';

for ( split /\s+/, $line ){
if( /^-i(.*)/ ){
print "$1\n";
}
}

__END__



--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
John Doe

2005-12-15, 6:57 pm

Umesh T G am Donnerstag, 15. Dezember 2005 15.31:
> Hi Joe,
>
> just to correct my prevoius one, my $line does not have * in the begining,
> that was a type. the $line value is like this
>
> *Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p -i"dir3\dir4" -m*
>
>
>
> my grep return the values correct to my array and the values in array will
> be like this obviously:
>
> *Options=-a"hello" -mt -ml3 *
> *"dir1\dir2"-k -p *
> *"dir3\dir4" -m*
> **
> **
> But, what I'm interested in getting the output values are:
> *"dir1\dir2"*
> *"dir3\dir4"*
>
> I do not know how to get them......can u suggest pls.?



use strict;
use warnings;

my $str=q(Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p -i"dir3\dir4" -m);

# this is only one possibility. I use \s* in the case spaces are
# allowed after -i.
# Instead of [^"]+ also .+? works.
# You could also modify the regex to allow " as well as ' around dirs.
#
my @dirs=$str=~m,-i\s*"([^"]+)",g;

print join "\n", @dirs;

# output:
dir1\dir2
dir3\dir4

ok?
:-)

[top post history following]
[color=darkred]
> On 12/15/05, John Doe <security.department@tele2.ch> wrote:
Umesh T G

2005-12-16, 3:55 am

Thanks Very much John. :) it worked, but a small problem. if I have "\"
after the directory name, it does not take as new instanance.
for ex:
if my $line is *Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p
-i"dir3\dir4\" -m -n -i"\dir5\dir6" -k*
**
then, I'm getting output like this,

dir1\dir2
dir3\dir4\dir5\dir6

This is because an extra "\" is present at the end of dir4.

can you help again pls.? thanks so much.

I want the output as

dir1\dir2
dir3\dir4
dir5\dir6



Umesh







On 12/15/05, John Doe <security.department@tele2.ch> wrote:
>
> Umesh T G am Donnerstag, 15. Dezember 2005 15.31:
> begining,
> will
>
>
> use strict;
> use warnings;
>
> my $str=q(Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p -i"dir3\dir4"-m);
>
> # this is only one possibility. I use \s* in the case spaces are
> # allowed after -i.
> # Instead of [^"]+ also .+? works.
> # You could also modify the regex to allow " as well as ' around dirs.
> #
> my @dirs=$str=~m,-i\s*"([^"]+)",g;
>
> print join "\n", @dirs;
>
> # output:
> dir1\dir2
> dir3\dir4
>
> ok?
> :-)
>
> [top post history following]
>
> extract
> -m*
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Umesh T G

2005-12-16, 3:55 am

Sorry for that John. It works fine.

Many thanks for your help.


Umesh


On 12/16/05, Umesh T G <umesh.perl@gmail.com> wrote:
>
> Thanks Very much John. :) it worked, but a small problem. if I have
> "\" after the directory name, it does not take as new instanance.
> for ex:
> if my $line is *Options=-a"hello" -mt -ml3 -i"dir1\dir2"-k -p
> -i"dir3\dir4\" -m -n -i"\dir5\dir6" -k*
> **
> then, I'm getting output like this,
>
> dir1\dir2
> dir3\dir4\dir5\dir6
>
> This is because an extra "\" is present at the end of dir4.
>
> can you help again pls.? thanks so much.
>
> I want the output as
>
> dir1\dir2
> dir3\dir4
> dir5\dir6
>
>
>
> Umesh
>
>
>
>
>
>
>
> On 12/15/05, John Doe <security.department@tele2.ch> wrote:
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com