JimiJ13
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.
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.
A slightly updated RegEx
stk:\s*(\d+\.?\d*)\s*cost:\s*(\d+\.?\d*)
Allows for variable spaces between elements and will match a single decimal only
ASKER
Julian Hansen (JulianH),
Thanks. But your suggestion gave empty result.
Thanks. But your suggestion gave empty result.
ASKER
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(subjectStri ng).Groups [1].Value;
string cost = regexObj.Match(subjectStri ng).Groups [2].Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
string subjectString = "wwww bs | stk: 15 Cost: 66.1";
try {
Regex regexObj = new Regex(@"stk: (\d+\.*\d*) Cost: (\d+\.*\d*)");
string stk = regexObj.Match(subjectStri
string cost = regexObj.Match(subjectStri
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
why not use split
Open in new window
Regards