
japhy
Enthusiast
/ Moderator
May 18, 2000, 9:28 AM
Post #2 of 4
(3138 views)
|
|
Re: Can someone explain this...
[In reply to]
|
Can't Post
|
|
Ok, you're doing something wrong with this regex, and I don't know what exactly you were trying to accomplish. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $string = "year2000/Month4/Week3/Test.html" $string =~ s/(^\/([^(\d)\/])*)((\d)+)(([^(\d)\/])*\/.*)/$1 - $2 - $3 - $4 - $5/; </pre><HR></BLOCKQUOTE> Let me dissect that regular expression for you, and perhaps that will help all of us figure out what's wrong. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> s{ ( # this starts $1 ^/ # match a / at start-of-string ( # this starts $2 [^(\d)/] # any characters EXCEPT (, ), /, and 0-9 )* # end $2, match 0 or more times ) # end $1 ( # this starts $3 ( # this starts $4 \d # a digit )+ # end $4, match 1 or more ) # end $3 ( # this starts $5 ( # this starts $6 [^(\d)/] # any characters EXCEPT (, ), /, and 0-9 )* # end $6, match 0 or more / # a / .* # and anything else ) # this ends $6 }{$1 - $2 - $3 - $4 - $5}x; </pre><HR></BLOCKQUOTE> Well, I still don't know totally what you were trying to do. It would help if you gave us your sample input string, "year2000/Month1/Week2/foo.html", and told us what you would like done to it. Are you trying to extract 2000, 1, 2, and "foo.html" from it? As for why 'r' and '0' are repeated, this is a particularly interesting regular expression anomoly: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> "ABCDEF" =~ /((.)+)/; print "$1 $2"; # prints "ABCDEF F" </pre><HR></BLOCKQUOTE> $2 is "F", and not "A". This is just how it works. If you want to extract the year, month, week, and filename, then I suggest you try: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> ($yr,$mon,$wk,$fn) = split !/!, $string, 4; for ($yr, $mon, $wk) { tr/0-9//cd } # remove non-digits from these </pre><HR></BLOCKQUOTE> Or, if you want to use a regular expression: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> ($yr,$mon,$wk,$fn) = $string =~ m{ year (\d+) / month (\d+) / week (\d+) / (.*) }xi; </pre><HR></BLOCKQUOTE>
|