newbie regxp question

J

Jesper

Hi
I'm trying to use regxp instead of split but I'm having some newbie troubles
with it. What I really want to accomplish is cutting the string:

"hello/world/I/am/a/newbie"

into:

"hello" and "world/I/am/a/newbie"

The problem is that the number of backslashes in the string varies from 3 to
? so I can't really make a regxp that does
m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

Regards, jesper
 
R

Ryan Shondell

Jesper said:
Hi
I'm trying to use regxp instead of split but I'm having some newbie troubles
with it. What I really want to accomplish is cutting the string:

"hello/world/I/am/a/newbie"

into:

"hello" and "world/I/am/a/newbie"

The problem is that the number of backslashes in the string varies from 3 to
? so I can't really make a regxp that does
m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

Well, from your description, it seems like you want the first "word"
before a backslash, and then everything else. How about...

m!(\w+)?/(.*)!


Ryan
 
D

David K. Wall

Jesper said:
I'm trying to use regxp instead of split but I'm having some
newbie troubles with it. What I really want to accomplish is
cutting the string:

"hello/world/I/am/a/newbie"

into:

"hello" and "world/I/am/a/newbie"

The problem is that the number of backslashes in the string varies
from 3 to ? so I can't really make a regxp that does
m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

perldoc -f split

Just split() it into two pieces:

my $string = "hello/world/I/am/a/newbie";
my ($first, $rest) = split '/', $string, 2;
print "$first, $rest";
 
J

Jürgen Exner

Jesper said:
I'm trying to use regxp instead of split

That sentence doesn't make sense. The first argument to split _is_ a regular
expression, so you must use a regexp in order to use split.
There is no "instead of".
but I'm having some newbie
troubles with it. What I really want to accomplish is cutting the
string:

"hello/world/I/am/a/newbie"

into:

"hello" and "world/I/am/a/newbie"

Then, what is wrong with
($hello, $rest) = split (/\//, "hello/world/I/am/a/newbie", 2);

jue
 
J

Jesper

Hi Ryan
That works like a charm - thx!
2 minuttes after I posted I got the following idea

m/([a-zA-Z0-9]*)\/(.*)/

What is the difference between m// and m!! ?

Regards, jesper


Ryan Shondell said:
Jesper said:
Hi
I'm trying to use regxp instead of split but I'm having some newbie troubles
with it. What I really want to accomplish is cutting the string:

"hello/world/I/am/a/newbie"

into:

"hello" and "world/I/am/a/newbie"

The problem is that the number of backslashes in the string varies from 3 to
? so I can't really make a regxp that does
m/(.*\/)(.*\/)(.*\/)(.*\/)(.*\/)/. Could anyone please help me ?

Well, from your description, it seems like you want the first "word"
before a backslash, and then everything else. How about...

m!(\w+)?/(.*)!


Ryan
--
perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
$;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
=shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
+shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
 
J

John W. Krahn

Jesper said:
Ok, fair enough - I just relate the =~ operator to regxp (newbie :)).

The binding operators (=~ and !~) are not strictly related to regular
expressions, they can also be used with tr/// which does not use regular
expressions at all.


John
 
J

Jürgen Exner

Jesper said:
Ok, fair enough - I just relate the =~ operator to regxp (newbie :)).

Hmmm, well, aehh, but the binding operator =~ doesn't have anything to do
with REs at all.
Of the top of my head there are only three operators/functions which use
REs:
- split
- s
- m
I'm sure someone will jump in if I forgot one.

Note: glob() and its relatives don't use REs but shell wildcards.

jue
 
J

Jürgen Exner

Jesper said:
2 minuttes after I posted I got the following idea

m/([a-zA-Z0-9]*)\/(.*)/

What is the difference between m// and m!! ?

If you use the slash then you can omit the "m".
Details see the man page for "m" ("perldoc -f m") which will point you to
the the perlop man page: "perldoc perlop"

jue
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top