Thursday, 5 September 2013

Regular Expression to detect Company Tickers using JAVA

Regular Expression to detect Company Tickers using JAVA

I am attempting to filter out company tickers from possible list of tickers.
Following code is what I got so far, I need to make the RegExp
sophisticated enough that only certain pattern is passed. See example code
below for more specific details.
Pattern tickerPattern = Pattern.compile("^[A-Z:\\.0-9]+$");
String[] tickerStrArr={
"JELK90#$", // NOT A TICKER
"1", // NOT A TICKER
"0", // NOT A TICKER
"R", // NOT A TICKER
"25.36", // NOT A TICKER
"1.0", // NOT A TICKER
"GOOG", // Ticker
"NYSE:C", // Ticker (with exchange code NYSE)
"GOOG.BY", // Ticker (with exchange code BY)
"$90", // NOT A TICKER
"98774", // Ticker (because more than 4
digit long)
"789.BY" // Ticker (because ends with
.[A-Z]{2,2})
};
for(String tickerStr: tickerStrArr)
{
Matcher matcher =tickerPattern.matcher(tickerStr);
if(matcher.find())
{
System.out.println("It's a ticker=>"+matcher.group());
}
}
Expected output
It's a ticker=>GOOG
It's a ticker=>NYSE:C
It's a ticker=>GOOG.BY
It's a ticker=>98774
It's a ticker=>789.BY
Can you formulate required RegExp which will get the above expected output?

No comments:

Post a Comment