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

Regular expression with capture following a match

by ramya2005 (Scribe)
on Sep 30, 2005 at 23:12 UTC ( [id://496551]=perlquestion: print w/replies, xml ) Need Help??

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

Hi PerlMonks:

I need to fetch the value next to 'speed' in the following string. 'speed basic-2.0 5.5 basic-11.0'.

I used a regular expression "speed \\w+\" which captured only the string "speed".

--------------------UPDATE---------------------------------------
I need a regular expression which prints everything after the word speed as one string.

my $string = "speed basic-2.0 5.5 basic-11.0" # I require a reg ex which can get me # my $required = "basic-2.0 5.5 basic-11.0"
-----------------END OF UPDATE------------------------------------
Any suggestions provided by you is much appreciated!

2005-10-02 Retitled by planetscape, as per Monastery guidelines
Original title: 'Regular expression'

<ramya2005>2005-10-03 Thanks for everyone for posting answers to my Regular expression question.
Since many people mis interpreted my question and posted answers in a different context I updated the original content.
Thanks for letting me know the guidelines. Now I added my original post content.

Replies are listed 'Best First'.
Re: Regular expression with capture following a match
by davido (Cardinal) on Sep 30, 2005 at 23:20 UTC
    "speed \\w+\"

    I don't see how that 'regexp' could capture 'speed'. It only will match successfully if the string contains the literal text "speed\w". That's because you've got a double-backslash in front of the 'w', so it's not functioning as a metacharacter, but rather as a literal character.

    As for a regexp that will capture whatever comes after 'speed', how about...

    m/speed(.+)/

    The regular expression could be made more specific as to what it captures if you define for us what part of the string following 'speed' constitutes "the value next to 'speed'".


    Dave

      You are right Dave.
      I actually had the reg ex speed (\\w+) in my code, which captured the string 'speed'.
      While typing in the Perl Monks I posted it as speed \\w+\" which was actually wrong.

        The regexp...

        m/speed (\\w+)/

        ...won't capture 'speed'. I don't know where you keep coming up with that idea. If that's how your regular expression looked, it's a mistake. Here is why:

        • Parenthesis mark matches that should be captured. The regexp you have shown is not trying to capture 'speed '. It's trying to capture '\\w+'.
        • \\w+ is still a mistake. The metacharacter that directs the regular expression engine to capture a word-like character is '\w'. When you add the second backslash, as in '\\w' you are directing the regular expression parser to treat that subexpression as literal characters, not a special metacharacter. The first backslash essentially 'escapes' the second one. The result is that you are asking the RE engine to match the literal characters '\w', and also telling the RE engine (via the + quantifier) that there may be one or more 'w' characters. You must remove the double-backslash, leaving only one backslash as in speed (\w+). But that still won't capture 'speed', it will capture what comes after speed, if it's a word grouping.

        Please read perlrequick and perlop. In perlop you'll want to read the gory details of quote and quote-like parsing. It may take awhile to sink in, but will help to understand the problem you're having with the double backslash.


        Dave

Re: Regular expression with capture following a match
by graff (Chancellor) on Sep 30, 2005 at 23:19 UTC
    If you have something that prints just "basic" from the input string you've shown us, I think it must be different from the regex you've shown us.

    You can use <code> and </code> tags around your data sample, and around the actual code that you have so far (not just the regex), and that will help others at the Monastery a lot.

    Meanwhile, to capture the next full token of "non-whitespace" characters following the string "speed " in your example, try this:

    if ( /speed (\S+)/ ) { $token = $1; # $token would be "basic-2.0" print "$token\n"; }
    Update: seeing davido's reply, I realize I misread the question -- he's right, you want to capture "(.*)" after "speed ", not just "(\S+)".
Re: Regular expression with capture following a match
by davidrw (Prior) on Sep 30, 2005 at 23:51 UTC
    what about:
    my ($everythingelse) = m/^speed (.+)/;
    What have you tried? Did you change the content of your original post? (i see the other replies talking about backslashes -- you shouldn't remove content once you've posted it ...)
Re: Regular expression with capture following a match
by Tanktalus (Canon) on Sep 30, 2005 at 23:23 UTC

    That's not a regex, that's a string. And the \'s aren't making a lot of sense. And that string used as a regex wouldn't match anything in your string. So I'm not entirely sure what you're trying. A code sample would be appreciated.

    my $str = 'speed basic-2.0 basic-11.0'; my ($one_basic) = $str =~ /speed\s*(\S+)/; my @all_basics = $str =~ /\s+(\S+)/g;

    Just guessing, though.

Re: Regular expression with capture following a match
by ioannis (Abbot) on Oct 01, 2005 at 00:12 UTC
    # This is the default, since we are not doing Unicode use bytes; $_ = 'speed basic-2.0 5.5 basic-11.0'; # In order to match 'anything', we must also match any newlines (my $result) = /\A \w+ \s+ (.*) \z/msxg; print $result;
Re: Regular expression with capture following a match
by Skeeve (Parson) on Oct 01, 2005 at 09:48 UTC
    why capture? Simply delete unneeded parts:
    ($everything_after_speed = $string) =~ s/.*speed\s+//s;

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Regular expression with capture following a match
by ww (Archbishop) on Oct 01, 2005 at 15:06 UTC
    From the comments immediately following the OP, it appears that ramya2005 may have edited the OP, making the replies useless.

    So, ramya2005, please, use strikethrus or some other method of updating; so those who come late to your posts can understand what you sought originally, and how you sought it.

      Sorry for changing the original content of the post. Since my question was not clear to many people I updated to make ir more clear. Thanks for pointing out the issues. Now I added the original content and update

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found