Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

matching string and get value

Posted on 2003-02-25
3
Medium Priority
?
140 Views
Last Modified: 2010-03-05
Hello
   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'.

0
Comment
Question by:aenriqgr
3 Comments
 
LVL 2

Expert Comment

by:perldork
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;
0
 
LVL 5

Accepted Solution

by:
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";
0
 

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

564 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