0

I am using the below expression for checking the stock symbol whether it is valid or not

$s_optionPattern = "/^[a-z0-9]{1,6}\s+(?:c|call|p|put)\s+[0-9\.]+\s+[0-9]{4}[0-9]{2}[0-9]{2}$/i";

$s_symbol   =   "TQNT CALL 6 20120519";
$s_symbol2  =   "BRK'B CALL 80 20120317";
echo "<br>--->".preg_match($s_optionPattern, $s_symbol);
echo "<br>--->".preg_match($s_optionPattern, $s_symbol2);

Here I am getting false (0) for second symbol and true for first symbol.

Is it correct if i edit the regular expression in the following manner.

$s_optionPattern = "/^[a-z0-9']{1,6}\s+(?:c|call|p|put)\s+[0-9\.]+\s+[0-9]{4}[0-9]{2}[0-9]{2}$/i";

But I am not sure it is correct or not. Can any body help me to fix this issue ?

Here some stock symbols have (dot,') characters also like BRK.B, BRK'B.

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
user1049997
  • 1,553
  • 4
  • 14
  • 18
  • It's also lacking PCRE delimiters, see manual or: possible duplicate of [converting ereg to preg.](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Mar 07 '12 at 09:57
  • In my case the stock symbols are capital letters only. – user1049997 Mar 07 '12 at 10:01
  • $s_optionPattern = "/^[a-z0-9]{1,6}\s+(?:c|call|p|put)\s+[0-9\.]+\s+[0-9]{4}[0-9]{2}[0-9]{2}$/i"; $s_symbol = "TQNT CALL 6 20120519"; $s_symbol2 = "BRK'B CALL 80 20120317"; echo "
    --->".preg_match($s_optionPattern, $s_symbol); echo "
    --->".preg_match($s_optionPattern, $s_symbol2);
    – user1049997 Mar 07 '12 at 10:05
  • There is an [edit link](http://stackoverflow.com/posts/9598944/edit) below your question. Purge comment, and move it there. – mario Mar 07 '12 at 10:07
  • 2
    To be sure if it is correct, try it! – Toto Mar 07 '12 at 10:12
  • I have tried and that is working fine. But i want to know adding (') character to the patter is correct or not. – user1049997 Mar 07 '12 at 10:24

1 Answers1

0

Try this

    $pattern='/[a-zA-Z.]+/';
    $s_symbol   ='TQNT';
    $s_symbol2  =   "BRK.B";
    preg_match_all($pattern,$s_symbol,$matches1);
    print_r($matches1[0]);
    preg_match_all($pattern,$s_symbol2,$matches2);
    print_r($matches2[0]);
kal
  • 48
  • 5