bnil
bnil

Reputation: 1541

Regular expression in c# to extract data

I want to extract the numbers from the string. I have two strings as below.

1_09-Sep-14#200

For the above string I am using the following expression but its not properly working, I want to get the 1 after 09-Sep-14 and then 200.

string S = "1_09-Sep-14#200";

foreach (Match m in Regex.Matches(S, "(?<=[_#])(\\d+)(?=[_#])?"))
{
    string s = Convert.ToString(m.Groups[1]);
}

I would like to use the regular expression for the this string as well.

1_4-11#100

Upvotes: 0

Views: 70

Answers (1)

vks
vks

Reputation: 67968

(?<=[_#]|^)([0-9a-zA-Z-]*)(?=[_#]|$)

Try this.

I have included ^ for capturing the first digit before _ and $ to capture last digit after #.

See demo.

http://regex101.com/r/nG1gU7/29

Upvotes: 1

Related Questions