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

I'm not sure how to do this, but . . .

OK, so I have a string called $aturl with a URL in it, for example "http://www.mysite.com/asdfom/adsf.htm". Then I have another string, $myuser, that has a domain in it such as "mysite.com". Now, I need to do two things:

1) See if $myuser is withing $aturl, regardless of case. I'm not too worried about this, since I believe I know how to convert a string to lowercase using a regex. I just put this here wondering if it could be fit into #2 (and henceforth the code optimized).

2) If $myuser is in $aturl, put everything _after_ $myuser into a seperate string. The location of $myuser needs to be regardless of case, but the new string needs to have the case it originally did in $aturl.

I hope I'm not bothering the Monks, but I really don't know where to start. I tried searching, but I didn't know what to search for. (If only Perl had MFC, I could do this in a heartbeat . . . sigh.)
  • Comment on Extracting a String after a String within a String . . .

Replies are listed 'Best First'.
Re: Extracting a String after a String within a String . . .
by djantzen (Priest) on May 29, 2003 at 01:07 UTC

    Regarding the first point, converting the case of a string using a regex is the hard way, just use uc or lc. To ignore case in a regex use the 'i' modifier.

    To the second point, what you want is to capture characters that appear at a certain place in the string, and you can do this using parentheses. For example:

    $aturl =~ /^http:\/\/$myuser(.*)$/i; # capture everything after the do +main $match = $1; # assign the captured stuff to a new variable.

    See perlre for more explanation.


    "The dead do not recognize context" -- Kai, Lexx
      Thanks, that's just what I was looking for.
Re: Extracting a String after a String within a String . . .
by Anonymous Monk on May 29, 2003 at 01:06 UTC
    $seperate = $1 if $aturl =~ /\Q$myuser\E(.*)/i;
Re: Extracting a String after a String within a String . . .
by WhiteBird (Hermit) on May 29, 2003 at 01:06 UTC
    You haven't really explained this well enough for me understand what exactly you are doing. So perhaps you should start with defining exactly what you want to do here.
    • Is the $aturl and $myuser always different in unpredictable ways? Or is there some way to know what these values will be?
    • Does $myuser show up in any consistent spot in the string?

    I would think that you would have to define some sort of predictable pattern to some part of it before you could build a regex to capture the part you want. And if you don't alter the case in any way when you break the string apart, you shouldn't have to worry about the case for the piece you grab.