×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to read the next word after a keyword found

How to read the next word after a keyword found

How to read the next word after a keyword found

(OP)
Hello everyone,

HOw can I read the next word after a keyword if found in a string.

i.e
The blue cat ( run over the car).

I like to say, if the word "cat" found, read the next character or word  which is in this case "(" or it could be a period or a ";" or even another word.

and ignoring any spaces
Thanks much

RE: How to read the next word after a keyword found

#! usr/bin/perl
$_="The blue cat ( run over the car).";
/cat\s/;
@after=split(/\s/,$');
print $after[0],"\n";


#$after[0] is what you want

RE: How to read the next word after a keyword found

my $string = "The blue cat ( run over the car).";
my $word = "cat";
$string =~ /$word\s*?(\S+)/;
my $next_word = $1;
print qq~The word after "$word" is "$next_word".~;

This will only find the first instance in the string.  If the string were "The big cat ate the little cat for dinner.", then it would stop after "cat ate".


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: How to read the next word after a keyword found

an addition to marblcat's reply

@after=split(/\s+/,$');

The addition of the + in the pattern let's it match one or more white-space characters, the other \s should be \s+ as well.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.

RE: How to read the next word after a keyword found

Oh yeah, that reminds me... by using \s*?, it will in fact return "fish" after "cat" if you had "catfish".  But by using + instead of *, then you only match the whole word instead of a part of the word.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: How to read the next word after a keyword found

A slight modification of Tom's regex:

$string =~ /cat\b\s*(\S+)/;

This will capture the '(' from your posted string and any word that follows cat, like 'in' from "cat in the hat". It will also capture a '.' or a ';' that follow cat (typically without a space), but won't capture 'alogue' from "catalogue" or 'fish' from "catfish". The '\b' matches a word boundary, which is the nonspace between a word character, \w, and a non-word character, \W.

jaa

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close