Pattern extraction

  • Thread starter Deepan - M.Sc(SE) - 03MW06
  • Start date
D

Deepan - M.Sc(SE) - 03MW06

I am having a string like below:

$str="/a/b/c/"; (or) $str = "/a/b/c/d/";

What i need is that i should always be able to extract "c" from the
above strings. I should not use split. Only by using regular
expressions i should be able to achieve this. Please help me to solve
this.

Thanks,
Deepan
 
T

Tad J McClellan

Deepan - M.Sc(SE) - 03MW06 said:
I am having a string like below:

$str="/a/b/c/"; (or) $str = "/a/b/c/d/";

What i need is that i should always be able to extract "c" from the
above strings. I should not use split.


Why not?
 
J

Jürgen Exner

Deepan - M.Sc(SE) - 03MW06 said:
I am having a string like below:

$str="/a/b/c/"; (or) $str = "/a/b/c/d/";

What i need is that i should always be able to extract "c" from the
above strings. I should not use split.

Why? It is the most natural tool for this task:

my $res = (split('/', $str))[3];
Only by using regular
expressions i should be able to achieve this.

1: the first argument of split() is a regular expressions
2: As stated the task is impossible because you need some other function
beside the RE to apply the RE, e.g. a s/// or m//

jue
 
C

Charlton Wilbur

D> I am having a string like below: $str="/a/b/c/"; (or) $str =
D> "/a/b/c/d/";

D> What i need is that i should always be able to extract "c" from
D> the above strings. I should not use split. Only by using
D> regular expressions i should be able to achieve this. Please
D> help me to solve this.

Split is the best way to solve this in Perl.

If this restriction is because that's what your teacher said, go ask
your teacher. He or she is being paid to teach you; we are not.

If this restriction is because you're using some language other than
Perl, which doesn't have a split function, ask in a forum appropriate
for that language.

Charlton
 
S

szr

Abigail said:
_
Deepan - M.Sc(SE) - 03MW06 ([email protected]) wrote on VCCCV
September
MCMXCIII in
<URL:"" I am having a string like below: ""
"" $str="/a/b/c/"; (or) $str = "/a/b/c/d/";
""
"" What i need is that i should always be able to extract "c" from
the "" above strings. I should not use split. Only by using regular
"" expressions i should be able to achieve this. Please help me to
solve "" this.


Easy piecy!


$str = "/a/b/c/"; # Or "/a/b/c/d/".
my $answer = substr $str, 5, 1;

say $answer;

#
# Dummy regexp to satisfy the homework requirements.
#
"" =~ /nananana/;


"say" ?

Can't call method "say" without a package or object reference


I am curious where this "say" came from?


$ perldoc say
No documentation found for "say".

$ perldoc -f say
No documentation for perl function `say' found

$ perldoc -q say
No documentation for perl FAQ keyword `say' found


Thanks.
 
J

John W. Krahn

szr said:
"say" ?

Can't call method "say" without a package or object reference


I am curious where this "say" came from?


$ perldoc say
No documentation found for "say".

$ perldoc -f say
No documentation for perl function `say' found

$ perldoc -q say
No documentation for perl FAQ keyword `say' found

say() is a new feature of Perl version 5.10 so you won't see it until
you upgrade.


John
 
S

szr

John said:
say() is a new feature of Perl version 5.10 so you won't see it until
you upgrade.

Thanks. I had a feeling. I am currently building 5.10.0 right now as I
type this :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top