matching string and get value

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-03-05
   I had one string and a text:
   The first is a patron e.g.: 'the X are blue'
   and i have a text e.g.:   'in the airport, the plane are blue"

      I need match the string with text and get the value
of X. In this case X='plane'.

Question by:aenriqgr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8020283
my $string = "The things are blue";

my $object = ($string =~ m/(\S+)\s+are blue/)[0];

print $object;

The (\S+) captures a sequence of non-space characters,
and the (/regexp/)[0] returns the captured value .. this can also be written

$string =~ m/(\S+)\s+are blue/;
my $object = $1;

Accepted Solution

PC_User321 earned 400 total points
ID: 8024155
$pattern = 'the X are blue';
$text = 'in the airport, the plane are blue';

die "Bad pattern\n" unless ($pattern =~ /^(.*)\bX\b(.*)$/);
$searchPattern = "$1(\\w+)$2";
die "Can't find a match\n" unless ($text =~ /$searchPattern/);
print "Answer is \"X = $1\"\n";

Author Comment

ID: 8025007
Thanks for your answer, your solution is perfect for my problems.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question