Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Search and replace help.

by SouKenji (Novice)
on Jul 10, 2011 at 21:02 UTC ( [id://913631]=perlquestion: print w/replies, xml ) Need Help??

SouKenji has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to do a bit of search and replace and for the life of me, I cannot figure out what I am doing wrong.

I have tried multiple iterations, ending with g, ge, r, re, but nothing seems to work. I always get the same output as my input.

Here is my current code etc:

$line= "F:\MF\BR\PreparedSources\acdv\test_dev\BeanName\SessionBean.RMWAppException.java"

if ($line=~ m/.java/) { $line=~ s/^(f:\\MF\\BR\\PreparedSources\\)([a-z]+\\)([a-z]+_dev)(\\) +([a-z]+)(.java)$/JAVAFILE "$4$5$6" "$2" "##All"/}</p>

Essentially, I want to take the text in $line and transform it to:

JAVAFILE "test_dev\BeanName\SessionBean.RMWAppException.java" "acdv" "##All"

Replies are listed 'Best First'.
Re: Search and replace help.
by philipbailey (Curate) on Jul 10, 2011 at 21:12 UTC

    There are two places where the left hand side of your s/// expression cannot match $line. That is:

    • f: will not match 'F:'
    • The third [a-z]+ will not match 'BeanName\SessionBean.RMWAppException'

    No match, therefore no replacement.

      Thanks Philip!

      I just noticed the f to F.

      I had tried .+ before, but to no avail.

      Switched the F and the a-z+ to .+ and it all works great!

      Thanks again.

Re: Search and replace help.
by Anonymous Monk on Jul 10, 2011 at 21:10 UTC

    add use re 'debug'; and you can view as the regex engine tells you what its matching and how it fails

    Also, use warnings, as they will tell you how $line doesn't contain what you think it does -- double quotes interpolate

      Thanks Anon, I had warnings on, the $line I mentioned is actually being pulled in from a file, so no quotes are actually around it. The debug thing is a great help though!

        Thanks Anon, I had warnings on, the $line I mentioned is actually being pulled in from a file, so no quotes are actually around it. The debug thing is a great help though!

        Aha. That is why its important (as How (Not) To Ask A Question would say) to clean your room, which is also part of How do I post a question effectively?

        #!/usr/bin/perl -- use strict; use warnings; use Test::More qw' no_plan '; Main( @ARGV ); exit( 0 ); sub Main { my $line = q[F:\MF\BR\PreparedSources\acdv\test_dev\BeanName\Sessi +onBean.RMWAppException.java]; my $olin = q[JAVAFILE "test_dev\BeanName\SessionBean.RMWAppExcepti +on.java" "acdv" "##All"]; is( $line, $line, '$line is $line, duh '); #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=913 +631;part=1 { use re 'debug'; $line=~ s/^(f:\\MF\\BR\\PreparedSources\\)([a-z]+\\)([a-z]+_de +v)(\\)([a-z]+)(.java)$/JAVAFILE "$4$5$6" "$2" "##All"/; } is( $line, $olin, '$line is $olin'); $line=~ s~ ^ # anchor start of line ( # $1 # f:\MF\BR\PreparedSources\ [^\\\/]+[\\\/] # f:\ [^\\\/]+[\\\/] # MF\ [^\\\/]+[\\\/] # BR\ [^\\\/]+[\\\/] # PreparedSources\ ) ( # $2 # acdv [^\\\/]+ ) [\\\/] ( # $3 # test_dev [^\\\/]+?_dev ) ( # $4 # \BeanName\SessionBean.RMWAppException.java [\\\/] .+ (?i-xsm:\.java) ) $ ## anchor end of line ~JAVAFILE "$3$4" "$2" "##All"~x; # //x means whitespace is insignificant is( $line, $olin, '$line is $olin for real'); } __END__ Compiling REx "^(f:\\MF\\BR\\PreparedSources\\)([a-z]+\\)([a-z]+_dev)( +\\)(["... Final program: 1: BOL (2) 2: OPEN1 (4) 4: EXACT <f:\\MF\\BR\\PreparedSources\\> (12) 12: CLOSE1 (14) 14: OPEN2 (16) 16: PLUS (28) 17: ANYOF[a-z][] (0) 28: EXACT <\\> (30) 30: CLOSE2 (32) 32: OPEN3 (34) 34: PLUS (46) 35: ANYOF[a-z][] (0) 46: EXACT <_dev> (48) 48: CLOSE3 (50) 50: OPEN4 (52) 52: EXACT <\\> (54) 54: CLOSE4 (56) 56: OPEN5 (58) 58: PLUS (70) 59: ANYOF[a-z][] (0) 70: CLOSE5 (72) 72: OPEN6 (74) 74: REG_ANY (75) 75: EXACT <java> (77) 77: CLOSE6 (79) 79: EOL (80) 80: END (0) anchored "f:\MF\BR\PreparedSources\" at 0 floating "_dev\" at 28..2147 +483647 (checking anchored) anchored(BOL) minlen 39 ok 1 - $line is $line, duh Guessing start of match in sv for REx "^(f:\\MF\\BR\\PreparedSources\\ +)([a-z]+\\)([a-z]+_dev)(\\)(["... against "F:\MF\BR\PreparedSources\a +cdv\test_dev\BeanName\SessionBean."... String not equal... Match rejected by optimizer not ok 2 - $line is $olin # Failed test '$line is $olin' # at D:\dev\misc\pm.913631.pl line 25. # got: 'F:\MF\BR\PreparedSources\acdv\test_dev\BeanName\Sessi +onBean.RMWAppException.java' # expected: 'JAVAFILE "test_dev\BeanName\SessionBean.RMWAppExcepti +on.java" "acdv" "##All"' ok 3 - $line is $olin for real 1..3 # Looks like you failed 1 test of 3.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://913631]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found