• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

parrent matching

I want to substitude the string

"string/another string/more string/WORLD/more string" to
"http://www.com/WORLD"

the only I know is that WORLD is placed
between 3-rd and 4-th slashes "/"

Thanks
0
kako
Asked:
kako
  • 3
  • 2
1 Solution
 
interiotCommented:
I don't quite understand your question, but here's a guess.

    $str1 = "string/another string/more string/WORLD/more string";
    $str2 = "http://www.com/" . (split '/', $str1)[3];

    print "$str2\n";
0
 
kakoAuthor Commented:
Yes, It will work, But I want to do the same
with the substitution command s///;
using pattern matching, I think there should be the
way to do so.
0
 
interiotCommented:
Ahh.  Well, pure s///s would be slow, because it uses backreferences, but anyway:

    $str1 =~ s|(?:[^/]*/){3}([^/]*).*|http://www.com/$1|;

And this is faster, and more readable:

    $str1 =~ s|([^/]*/){3}||;
    $str1 =~ s|/.*||;
    $str1 = "http://www.com/$str1";
0
 
kakoAuthor Commented:
Yes, It will work, But I want to do the same
with the substitution command s///;
using pattern matching, I think there should be the
way to do so.
0
 
interiotCommented:
s||| is the same as s///, so is the aforementioned line an acceptable solution?

    $str1 =~ s|(?:[^/]*/){3}([^/]*).*|http://www.com/$1|;
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now