开发者

perl and regex to extract a path

开发者 https://www.devze.com 2023-03-15 20:00 出处:网络
I have a block of text/code where I need to search for include= and then grab the entire path after the \'=\' I\'ve tried several ways and I just can\'t seem to get what I\'m looking for in the chunk

I have a block of text/code where I need to search for include= and then grab the entire path after the '=' I've tried several ways and I just can't seem to get what I'm looking for in the chunk of code/text:

include=Y:\default\main\tsconfig\custom\inline\callouts\search\results.

assume this is the xml being searched:

<item name="Post Include Code">
<value>
First section of content

include=Y:\default\main\tsconfig\t开发者_开发知识库sconfig\custom\inline\callouts\search\results.tpl
Second section of content
</value>

</item>

The code:

if ($includeText=~ s/include=(.*)$/\1/) { 
    print "$1";
}
else { 
    print "no path";
}

I want to end up with Y:\default\main\tsconfig\custom\inline\callouts\search\results.tpl so that I can include a template. The text/code is pulled from another file.

Thanks!


I made a small change in your script and its working:

$include = "include=Y:\\default\\main\\tsconfig\\custom\\inline\\callouts\\search\\results.";


if ($include =~ m/include=(.*)$/) { 
    print "$1";
}
else { 
    print "no path";
}


Sorry mate, I took your script, used it, and it works just fine. There is nothing wrong with your regex, but rather with your other code, which you have not shown.

Also: A bit more info about what your problem is would be great. "I just can't seem to get.." doesn't really tell us much about what's going wrong.

My guess is that you are not using:

use strict;
use warnings;

And that you therefore have done something silly such as:

while ($includetext = <DATA>) {
    if ($includeText =~ s/...)

This script works:

use warnings;
use strict;

while (<DATA>) {
    if (s/include=(.*)$/$1/) { 
            print "$1";
    }
    else { 
            print "no path";
    }
}

__DATA__
include=Y:\default\main\tsconfig\custom\inline\callouts\search\results.


i think this is what you are looking for

#!/usr/bin/perl
use strict;
use diagnostics;
my $inc='include=Y:\default\main\tsconfig\custom\inline\callouts\search\results.tpl';
if ($inc =~ m{include=([\w\:\\\w /]+\w+\.\w+)} )
{
print $1;
}
else
{
print "no path";
}


You don't want to include the "include=" part. You want just what's after, so you'd have to exclude the "include=" part with something like this: [^include=].* (a quick text in a text editor worked for me).


//EDIT: corrected after looking at a few examples This seems to work, this is same as Raghuram's post.

if($includeText =~ m/include=(.*)$/){
                    print "$1";
                    }
                    else { 
                        print "no path";
                    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号