1

Text.RegularExpressions.Regex class in c#, in this class I am trying to find string that contains "years" word within

 string s11 ="java developer in new york 2years exp"

before i am using

Regex = new Regex("(\d+)(years?)");

here i am getttig inter value in the given string of s11 like

Match match = regex.Match(ss);

if(match.Success)
{
    string s = match.Groups[1];
    string s1 = match.Groups[2];
}

but this one only works with integer, if suppose I change my string, like:

string s11="java developer in new york 0.10years exp"

it stops working.

I want work with both integer or decimal like

    string s11="java developer in new york 2years exp"
    string s11="java developer in new york 0.10years exp"

can any body give the correct solution for this problem; that is to look for both integer and decimal value.

Thanks pradeep

Otiel
  • 18,404
  • 16
  • 78
  • 126

3 Answers3

2

Yours is close; I think if you simply add the optional decimal parts:

Regex = new Regex("((?:\d*\.)?\d+)(years?)");
Kenneth K.
  • 2,987
  • 1
  • 23
  • 30
1

What about ?

Regex = new Regex("([0-9]?\.?\d+)\s*(years?)");

Also, there are a number of tools which let you try them out interactively, like

http://visualstudiogallery.msdn.microsoft.com/16b9d664-d88c-460e-84a5-700ab40ba452

jpmuc
  • 1,092
  • 14
  • 30
1

Try this

new Regex(@"([0-9]*\.[0-9]+|[0-9]+)*years");

Also, for future reference, http://regexpal.com/ is a simple tool to test out your regular expressions and http://www.regular-expressions.info gives lots of examples.

Kevin Brydon
  • 12,524
  • 8
  • 46
  • 76