How to extract number string based on specific code

Dear Experts,

I have this generic string:
wwww bs | stk: 15 Cost: 66.1

How can I extract the  stk & cost based on the above string?


Thanks.
JimiJ13I T ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
string subjectString = "wwww bs | stk: 15 Cost: 66.1";
try {
	Regex regexObj = new Regex(@"stk: (\d+\.*\d*) Cost: (\d+\.*\d*)");
	stk = regexObj.Match(subjectString).Groups[1].Value;
        cost = regexObj.Match(subjectString).Groups[2].Value;
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}

Open in new window

HTH,
Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rgonzo1971Commented:
HI,

why not use split

      var names = "wwww bs | stk: 15 Cost: 66.1".Split(' ');
      var stk = names.GetValue(4);
      var Cost = names.GetValue(6);

Open in new window

Regards
0
JimiJ13I T ConsultantAuthor Commented:
Dan Craciun,
I tried your suggestion and these are the errors:

2 The name 'stk' does not exist in the current context      
2 The name 'cost' does not exist in the current context      

Any idea?

Thanks.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

JimiJ13I T ConsultantAuthor Commented:
Rgonzo1971,

When I tried it in the following strings and it did not work:

"The best bs | stk: 15 Cost: 66.1".Split(' ');
"Thebestbs | stk: 15 Cost: 66.1".Split(' ');
"The best is yet | stk: 15 Cost: 66.1".Split(' ');

Thanks.
0
Julian HansenCommented:
A slightly updated RegEx
stk:\s*(\d+\.?\d*)\s*cost:\s*(\d+\.?\d*)

Open in new window

Allows for variable spaces between elements and will match a single decimal only
0
JimiJ13I T ConsultantAuthor Commented:
Julian Hansen (JulianH),

Thanks. But your suggestion gave empty result.
0
JimiJ13I T ConsultantAuthor Commented:
Great! it works with minor modification:

string subjectString = "wwww bs | stk: 15 Cost: 66.1";
try {
      Regex regexObj = new Regex(@"stk: (\d+\.*\d*) Cost: (\d+\.*\d*)");
      string stk = regexObj.Match(subjectString).Groups[1].Value;
        string cost = regexObj.Match(subjectString).Groups[2].Value;
} catch (ArgumentException ex) {
      // Syntax error in the regular expression
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.