• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

REGULAR EXPRESSION FOR STOCK QUOTE

Dear Gurus,

I need a way to match the following format using regular expression

A-Z meaning only capital minimum 3 maximum 5..followed by ( then positive numeric ) again ( followed by - or + integer and again )

when I have for example ABBL370(100)(-10)

I need to break it down into structure

struct {

string stockCode;
int stockPrice;
int stockVolume;
int stockChange;
}

so that I can dump it into a database table;

Thank you..

Asof2010-01-0614:32:45ABBL370(100)(-10)AHPC565(30)(19)BDBL270(20)(10)BOAN480(10)(8)BOK915(100)(-10)BPCL870(100)(-16)CEDBL780(10)(20)CFL393(50)(-7)CIT780(22)(0)CMBSL310(60)(5)CZBIL573(20)(-7)DCBL305(43)(1)GBL320(30)(5)GFCL329(300)(-3)GFL356(25)(-7)HBL1,020(102)(-10)ICFC372(100)(-5)ILFC203(300)(0)KBL515(114)(-5)KIST301(500)(-4)LUBL320(51)(2)MBL322(85)(-7)NABIL2,561(46)(-14)NBB285(1000)(-3)NCCB282(200)(-8)NCM715(32)(0)NDEP230(60)(-9)NDFL295(30)(5)NIB848(52)(8)NICB640(94)(-10)NMB318(66)(0)NTC500(100)(0)OFL408(158)(-8)PCBL578(140)(-12)PFL412(100)(-8)PRFL470(15)(-1)PSDBL495(80)(5)RIBSL490(50)(8)SBBL560(100)(-15)SBI870(466)(-3)SBL462(100)(-9)SBSL275(20)(6)SCB3,381(63)(6)SDBL222(540)(0)SIC246(58)(-5)SMBF374(15)(-9)SRBL391(20)(8)STFL252(200)(2)TBBL670(10)(-12)VBBL369(60)(-3)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;


namespace HTMLmake
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HtmlAgilityPack.HtmlWeb xyz = new HtmlAgilityPack.HtmlWeb();
            HtmlAgilityPack.HtmlDocument abc = xyz.Load("http://www.nepalstock.com");
            HtmlAgilityPack.HtmlNodeCollection htnodecol = abc.DocumentNode.SelectNodes("//marquee");
            string htmlinnertext;
            //Regex objRegex = new Regex();
            
            foreach(HtmlAgilityPack.HtmlNode link in htnodecol)
            {
                htmlinnertext = link.InnerHtml;  //DISPLAY STOCK TICKER
                htmlinnertext = Regex.Replace(htmlinnertext, "<.*?>", string.Empty);
                htmlinnertext = Regex.Replace(htmlinnertext, "&nbsp;", string.Empty);
                htmlinnertext = Regex.Replace(htmlinnertext, " ", string.Empty);
                Response.Write(htmlinnertext);

                //EXTRACT TIME ^([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-9][0-9])$
                Response.Write(Regex.Match(htmlinnertext, "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"));


                //EXTRACT DATE ^([0-9][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])$     //MATCH:2010-01-06
                Response.Write(Regex.Match(htmlinnertext, "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$"));


            }
        }
    }
}

Open in new window

0
rotatingluficer
Asked:
rotatingluficer
  • 5
  • 3
1 Solution
 
DocSeltsamCommented:
Hi there,

try this one ([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)

--TheDoctor
0
 
rotatingluficerAuthor Commented:
foreach (Match m in Regex.Matches(htmlinnertext, "([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)"))
                     
                                  I've tried this one before.. can u please tell me why \(   escape sequence followed by ( or \) gives me unrecognized escape sequence.. Thanx Doc.. your ur input..  I've tried ur same code with \\(  I'm going nutts..:)

Thanx you.. if u fix the unrecognized escape sequence.. I'll accept ur answer..
escape.jpg
0
 
DocSeltsamCommented:
Hi there,

withinn CS you can suppress the need for escaping by prefixing an @ to you string.

foreach (Match m in Regex.Matches(htmlinnertext, "([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)"))
                     
works fine for me here.


--TheDoctor

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DocSeltsamCommented:
Sorry,

accidently hit the enter key ....

I mean

foreach (Match m in Regex.Matches(htmlinnertext, @"([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)") )

:)

--TheDoctor
                     
0
 
rotatingluficerAuthor Commented:
Thanx Doc..
What about exploding ABBL370(170)(-10) into

struct xyzdb {
string stockCode;
int stockPrice;
int stockVolume;
int stockDeviation;

};


Thanx you.. the code is working I didn't know @ was required..

0
 
DocSeltsamCommented:
Hi there, try the following snippet,
where str is your string and theData is your struct.

BTW, my regExp was a bit wrong, yui need to use this one.
@"([A-Z]{3,5})(\d*)\((\d*)\)\(([-\d]*)\)"
                                      ^^^^
The marked point was flipped a bit.

--TheDoctor
foreach ( Match m in Regex.Matches( str , @"([A-Z]{3,5})(\d*)\((\d*)\)\(([-\d]*)\)" ) )
{
    if (m.Success)
    {
        // m.Groups[0].Value keeps the complete match

        theData.stockCode   = m.Groups[1].Value;
        theData.stockPrice  = int.Parse(m.Groups[2].Value);
        theData.stockVolume = int.Parse(m.Groups[3].Value);
        theData.stockChange = int.Parse(m.Groups[4].Value);
    }
}

Open in new window

0
 
rotatingluficerAuthor Commented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;


namespace HTMLmake
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
       
        protected void Button1_Click(object sender, EventArgs e)
        {
            HtmlAgilityPack.HtmlWeb xyz = new HtmlAgilityPack.HtmlWeb();
            HtmlAgilityPack.HtmlDocument abc = xyz.Load("http://www.nepalstock.com");
            HtmlAgilityPack.HtmlNodeCollection htnodecol = abc.DocumentNode.SelectNodes("//marquee");
            string htmlinnertext, temp;
            string[] taker;
            //Regex objRegex = new Regex();
           
           
            foreach (HtmlAgilityPack.HtmlNode link in htnodecol)
            {
                htmlinnertext = link.InnerHtml;  //DISPLAY STOCK TICKER
                htmlinnertext = Regex.Replace(htmlinnertext, "<.*?>", string.Empty);
                htmlinnertext = Regex.Replace(htmlinnertext, "&nbsp;", string.Empty);
                htmlinnertext = Regex.Replace(htmlinnertext, " ", string.Empty);
                htmlinnertext = Regex.Replace(htmlinnertext, ",", string.Empty);
           
              //  Response.Write(htmlinnertext);
              //  Response.Write(Regex.Match(htmlinnertext, "([0-9]{4})(-)([0-9]{2})(-)([-9]{2})"));
                foreach (Match m in Regex.Matches(htmlinnertext, @"([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)"))
                {
                    temp = m.ToString();
                    temp = Regex.Replace(temp, @"\(", ",");
                    temp = Regex.Replace(temp, @"\)", " ");
                    taker = temp.Split(',');
                    Response.Write(temp + "<br>");
                    Response.Write(Regex.Match(taker[0],"([A-Z]{3,5})").ToString() + "<br>");
                    Response.Write(Regex.Match(taker[0], "[0-9]+").ToString() + "<br>");
                    Response.Write(taker[1] + "<br>");
                    Response.Write(taker[2] + "<br>");
                }
                //EXTRACT TIME ^([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-9][0-9])$
             //   Response.Write(Regex.Match(htmlinnertext, "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$"));


                //EXTRACT DATE ^([0-9][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])$     //MATCH:2010-01-06
             //   Response.Write(Regex.Match(htmlinnertext, "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$"));


            }
        }
    }
}
0
 
DocSeltsamCommented:
Hi there,

in your code, you still have the wrong regexp.

You use              @"([A-Z]{3,5})(\d*)\((\d*\))\(([-\d]*)\)")
but u should use @"([A-Z]{3,5})(\d*)\((\d*)\)\(([-\d]*)\)")

Mind the part with the 2nd \d
otherwise you get the closing bracket from the 2nd number ( eg '123)' instead of '123' )

--TheDoctor
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now