RegEx for newbie

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

Im new to C# regEx.
I want to do a validation in array of integers and alphanumeric, to know
each item is a number between 250 and 350 using regEx.
string[] Arr= new string[] {"191", "677", "31", "901", "250", "100", "313",
"219", "1234567", "abc123"};



thanks,
Praveen
 
Hi Praveen,
I want to do a validation in array of integers and alphanumeric, to know
each item is a number between 250 and 350 using regEx.
string[] Arr= new string[] {"191", "677", "31", "901", "250", "100",
"313", "219", "1234567", "abc123"};

If you know all (or most) of your strings are just numbers in string format,
I would probably convert the numbers to an integer, and then compare the
result with 250 and 350.

However if you still would want to use regex patterns, here's basically what
you need:

- if the number starts with 2, allow numbers 5-9 in 2nd position and any
number from 0-9 in the 3rd, or
- if the number starts with 3, allow numbers 0-4 in 2nd position and 0-9 in
the 3rd, or
- if the number starts with 3, allow number 5 in 2nd position and 0 in the
3rd.

If my morning brain works OK, that should do it. Here's also a nice site
that I've found helpful:

http://www.regular-expressions.info/tutorial.html

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Im new to C# regEx.
I want to do a validation in array of integers and alphanumeric, to know
each item is a number between 250 and 350 using regEx.
string[] Arr= new string[] {"191", "677", "31", "901", "250", "100", "313",
"219", "1234567", "abc123"};

Included a little piece of code as an example.
The regular expression for your problem is:

^(2[5-9][0-9])|(3([0-4][0-9])|(50))$


-----------------------------------------------
private void CheckInput()
{
StringBuilder sb = new StringBuilder();
string[] arr = new string[]
{ "191", "677", "31", "901", "250", "100", "313", "219",
"1234567", "abc123" };
foreach ( string s in arr )
{
sb.Append( s );
sb.Append( " evaluates to " );
sb.Append( IsValid( s ) );
sb.Append( "\n" );
}
MessageBox.Show( sb.ToString() );
}

private bool IsValid( string s )
{
Regex re = new Regex( "^(2[5-9][0-9])|(3([0-4][0-9])|(50))$" );
return re.IsMatch( s );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top