Jump to content

Recgnize Stock Symols (3 letters in parenthesis)


Recommended Posts

I have a bunch of text, and I'm looking to recognize 2-5 UPPER CASE letters within a paragraph.  A sample paragraph is this:

 

==================

United Airlines' parent UAL (UAUA) sprang a surprise in July when it posted second-quarter earnings that handily beat Street forecasts. Another surprise was how the stock has bounced up after UAL emerged from bankruptcy in February, 2006--despite high oil prices, rising competition, and public outcry about the airlines' disruptive flight cancellations and delays. The stock climbed from 21 in February to 49 on July 23, 2007, the day before the market's plunge.

==================

 

The regular expression that I'm currently using is:  \(.*?\)

 

However, it recently failed on the following paragraph (for obvious reasons).  Regular Expressions are just so pesky!

 

======= FAILED ON THIS PARAGRAPH =======

Sweden's Elekta, which trades only in Stockholm, is getting more attention from American investors. (Its Bloomberg symbol is EKTAB.) Some 30% of its stock is owned by U.S. institutions, including Fidelity Investments and Lazard (LAZ). And nearly half of the global sales of Elekta's cancer radiation equipment are in the U.S. In that market Elekta is second only to Varian Medical Systems (VAR) in Palo Alto, Calif. No. 3 is Siemens (SI).

=====================================

 

It picked up the following: Its Bloomberg symbol is EKTAB.

I was HOPING it would only pick up: EKTAB

 

Any help would be greatly appreciated!

(2-5) is looking to capture the literal "2-5"; what you're looking for is an interval: {2,5}.

 

This should work as long as you only expect to find one stock symbol within a set of parentheses. If you need to look for more, you'll need to perform two searches.

 

<pre>
<?php
$tests = array(
	"United Airlines' parent UAL (UAUA) sprang a surprise in July when it posted second-quarter earnings that handily beat Street forecasts. Another surprise was how the stock has bounced up after UAL emerged from bankruptcy in February, 2006--despite high oil prices, rising competition, and public outcry about the airlines' disruptive flight cancellations and delays. The stock climbed from 21 in February to 49 on July 23, 2007, the day before the market's plunge.",
	"Sweden's Elekta, which trades only in Stockholm, is getting more attention from American investors. (Its Bloomberg symbol is EKTAB.) Some 30% of its stock is owned by U.S. institutions, including Fidelity Investments and Lazard (LAZ). And nearly half of the global sales of Elekta's cancer radiation equipment are in the U.S. In that market Elekta is second only to Varian Medical Systems (VAR) in Palo Alto, Calif. No. 3 is Siemens (SI)."
);
foreach ($tests as $test) {
	preg_match_all('/
		\(
			(?:
				(?[A-Z]{2,5})|[^)]?)
			)*
		\)
	/x', $test, $matches);
	print_r($matches);
}
?>
</pre>

ok in regex you can use parenthes to capture a part of the string but sometime you want to group a few letters but are not going to use them so to speed things up you use (?:blar) instead of (blar)..

 

ie

$theData = "hello world";
preg_match_all('/(hello)(world)/si', $theData, $result, PREG_PATTERN_ORDER);
$result = $result[0];
print_r($result);

returns an array

[0] = hello

[1] = world

 

$theData = "hello world";
preg_match_all('/(?:hello)(world)/si', $theData, $result, PREG_PATTERN_ORDER);
$result = $result[0];
print_r($result);

returns an array

[0] = world

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.