views:

99

answers:

4

I will have a different type of string(string will not have fixed format,they will be different every time) from them I want to remove some specific substring.Like the string can be

OPTIDX 26FEB2009 NIFTY CE 2500
OPTIDX NIFTY 30 Jul 2009 4600.00 PE
OPTSTK ICICIBANK 30 Jul 2009 700.00 PA

I want to extract Rs.(digit) from those string and store it into one variable and then in those string there should not be Rs.(digit).

What should be the regex for that ?

EDIT

private string ExtractingRupeesFromString(String str)
{
    Match match = Regex.Match(cellRecord, @"(\d+(?:\.\d+)?)\D*$");
    return match.Value.ToString();
}

for(int i=0;i<datagridview1.Rows.Count;i++)
{
    datagridview1["Rs",i].Value=ExtractingRupeesFromString(datagridview1["ScriptName",i].Value.ToString());
}
A: 

Regex ist the best for this.

Jack
I have updated the question with the help of the advise of jack30lena.So please do not minus reputation for **jack30lena**.
Harikrishna
A: 

I agree with jack30lena. This can be easy done with an regular expression.

for example:

Regex blabla = new Regex(@"([^0-9]*(?<number>[0-9])+)*");
MatchCollection matches = blabla.matches("OPTIDX 26FEB2009 NIFTY CE 2500");
foreach(Match m in matches)
{
string number = m.Groups["number"].Value;
//do something with the match...
}
MuSTaNG
+2  A: 

If you wan't to extract only ['2500', '4600.00', '700.00'], you could use following regex with MULTILINE flag enabled

@"([+-]?\d+(?:\.\d+)?)\D*$"

Edit: Added optional [+-], and some C# codes

string s = @"
OPTIDX 26FEB2009 NIFTY CE 2500
OPTIDX NIFTY 30 Jul 2009 4600.00 PE
OPTSTK ICICIBANK 30 Jul 2009 700.00 PA
";
Regex re = new Regex(@"([+-]?\d+(?:\.\d+)?)\D*$",RegexOptions.Multiline);
foreach (Match a in re.Matches(s)){
    System.Console.WriteLine(a.Groups[1]);
}

//2500
//4600.00
//700.00

Edit:

You could change

return match.Value.ToString();

to

return match.Groups[1].ToString();

to return only number part.

S.Mark
@S.Mark,How is `[-+]?[0-9]*\.?[0-9]+` ?
Harikrishna
@Harikrishna, ok, if there is + or - infront of those, thats good idea. but `[-+]?[0-9]+(\.[0-9]+)?` would be better, because decimal part could be ignored.
S.Mark
@Harikrishna, do you use the one in my c# example? `@"([+-]?\d+(?:\.\d+)?)\D*$"`
S.Mark
@S.Mark,Yes sir...Sorry for that.. It works fine.
Harikrishna
You're welcome, Harikrishna.
S.Mark
@S.Mark,But it matches with `-1300.00`.
Harikrishna
@Harikrishna, `-1300.00` is matched because `[+-]?` , if `-` or `+` is not need , remove `[+-]?`
S.Mark
@S.Mark,Ok Sir,Thank You Very Much....
Harikrishna
You're welcome again. Harikrishna
S.Mark
@Harikrishna, `\D*$` is to match `PA` after `1300.00` and to make sure to match *last* numeric value in a certain line. If you remove those parts, 2009 and other numbers will match, so its not good idea to remove those parts.
S.Mark
@Harikrishna, make sure you use `Groups[1]` to match only `1300.00` instead of `1300.00 PA`
S.Mark
@S.Mark,Then any other approach to let do not match with `PA`.
Harikrishna
@S.Mark,Sir I am checking it one by one not by groups.
Harikrishna
@Harikrishna, can I see the code you using? and a little more details? You could update it in question or could post another one, if the question is different.
S.Mark
@S.Mark,Ok Sir...I am updating question..
Harikrishna
@Harikrishna, I've updated my answer too.
S.Mark
@S.Mark,Sir,You are great !! It works....
Harikrishna
@Harikrishna, Glad to hear that :-)
S.Mark
A: 

How is

Match match = Regex.Match(str, @"[-+]?[0-9]*\.?[0-9]+");
Rupees=str.Substring(match.Index,match.Length);

Is it ok ? Please check the regex.

Harikrishna