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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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
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.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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.
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
JimiJ13I T ConsultantAuthor Commented:
Julian Hansen (JulianH),

Thanks. But your suggestion gave empty result.
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
}
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.