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

I have a data in a line like this:

this is a test /test123?

I want to get the data between / and ? I tried to do the regular expression ($content) = $item1 =~ m/ \/(.*) \? /; but doesn't seem to be working. What am I doing wrong? Thanks a lot

Replies are listed 'Best First'.
Re: How do i get string between
by ikegami (Patriarch) on May 03, 2010 at 21:15 UTC
    The pattern requires a space before the question mark, but there is no space there in the string against which you are matching.
Re: How do i get string between
by kennethk (Abbot) on May 03, 2010 at 21:04 UTC
    but doesn't seem to be working. What am I doing wrong?

    "not working" can mean a lot of things to a lot of people. Please take a look at I know what I mean. Why don't you? or How do I post a question effectively? so we can better understand your issue. Posting code with sample input and expected output is very helpful in diagnosing problems.

    Based on what you've posted, I'm guessing the issue you are having is with greedy matching. .* matches as much as it can, whereas .*? grabs the shortest string that meets the criteria. So perhaps the code

    ($content) = $item1 =~ m/\/(.*?)\?/;

    does what you intend? See perlretut for more info.

    Also possibly at fault is white space I see in your posted expression. Of course, that might just be an artifact of the fact that you didn't wrap your code in <code> tags; see Writeup Formatting Tips.

Re: How do i get string between
by toolic (Bishop) on May 04, 2010 at 00:14 UTC
    Others have pointed out problems with your code.

    Here is another way to do it:

    use strict; use warnings; my $item1 = 'this is a test /test123?'; my ($content) = $item1 =~ m{ / (.*) \? }x; print "$content\n" if defined $content;
    • Using alternate delimiters m{} allows you to avoid escaping the slash /
    • Using the x modifier allows you to separate the components of your regular expression with whitespace for clarity.
    • It is a good practice to check if your match succeeded. I used defined.

      Better to test the match result. Consider:

      #!/usr/bin/perl use strict; use warnings; for my $str ('Matched', 'No match') { $str =~ /(\w*ed)/; print "defined: Matched $1 for '$str'\n" if defined $1; } for my $str ('Matched', 'No match') { print "match: Matched $1 for '$str'\n" if $str =~ /(\w*ed)/; }

      Prints:

      defined: Matched Matched for 'Matched' defined: Matched Matched for 'No match' match: Matched Matched for 'Matched'
      True laziness is hard work
Re: How do i get string between
by kiruthika.bkite (Scribe) on May 04, 2010 at 12:54 UTC
    your code,
    this is a test /test123? $item1 =~ m/ \/(.*) \? /;
    As per your regular expression ,searched pattern requires space before and after '?'.

    But here searched pattern is not having space before and after '?'.
    So use the regular expression,
    $item1 =~/\/(.*)\?/;
    $1 will have test123.
Re: How do i get string between
by metaperl (Curate) on May 03, 2010 at 21:00 UTC
Re: How do i get string between
by metaperl (Curate) on May 03, 2010 at 21:04 UTC